2016-01-20 112 views
1

我已经做了一次不合算的尝试来合并来自两个不同表格的值。第一个显示就像我想的那样,但第二个显示的只是第一行。在不使用mysql连接的情况下合并两个表?

Select * From (Select date as Date, Sum(qty) as qtySum, Sum(weight) 
as weightSum From stock_list Group by date) as A, 
(Select Sum(weight) as weightSum,Count(barcode) 
as barcodeCount From selected_items Group by date) as B Group by date; 

这是我得到的输出。

enter image description here

这些是我selected_items表。

enter image description here

这是我的stock_list。 enter image description here

我的两个单独的查询工作,我得到了正确的输出只有当我试图运行它们放在一起就给出了第二query.Can任何一个问题指出我的错误或告诉我一个更好的方式来做到这一点。 这就是我的最终目标是enter image description here

+0

谢谢@Jens你能帮助解决方案。 – AndroidNewBee

+0

为什么你不使用连接? – Jens

+0

为什么不使用'JOIN'运算符?从表A和表B到日期栏的简单FULL JOIN结果。 – fabulaspb

回答

1

第一个问题是,你按日期在你的子查询B分组,但你不选择它,所以你的结果集可能是这样的:

weightSum  barcodeCount 
--------------------------- 
26    8 
9    14 
4    7 

这是结果3个日期,但你不知道哪一行是指哪个日期。

你的下一个问题是,您使用的是交叉连接,这是因为你的两个查询之间没有联系,这意味着如果你的第一个查询返回:

Date   qtySum  weightSum 
---------------------------------------- 
2016-01-20  1   1 
2016-01-21  2   2 

你做了之后这个交叉连接你结束

Date   qtySum  a.weightSum  b.weightSum  barcodeCount 
-------------------------------------------------------------------------- 
2016-01-20  1   1    26    8 
2016-01-20  1   1    9    14 
2016-01-20  1   1    4    7 
2016-01-21  2   2    26    8 
2016-01-21  2   2    9    14 
2016-01-21  2   2    4    7 

所以A的每一行都与B的每一行相匹配,给你6个总行。

你的第三个问题是,你然后按日期分组,但不执行任何聚合,没有深入研究SQL标准,group by子句和函数依赖关系的细节,让它简化为MySQL允许这样做,但除非你明白限制(这在this answer的更详细的介绍中),否则你不应该这样做。 select中不在group by子句中的任何内容可能应该在聚合中。因此,由于MySQL的GROUP BY扩展通过选择所有内容并仅按日期分组,所以您实际上说的是每个日期取1行,但您无法控制哪一行,它可能是每行的第一行如上面显示的,所以你会得到的结果是组:

Date   qtySum  a.weightSum  b.weightSum  barcodeCount 
-------------------------------------------------------------------------- 
2016-01-20  1   1    26    8 
2016-01-21  2   2    26    8 

我想这是为什么你从子查询B中的所有相同的价值观结束了重复。

因此,这涵盖了什么是错误的,现在解决方案,假设stock_list中的日期在selected_items中不存在,反之亦然,您将需要一个完整的连接,但是因为这在MySQL中不受支持你将不得不使用UNION,最简单的方法是:

SELECT t.Date, 
     SUM(t.StockQuantity) AS StockQuantity, 
     SUM(t.StockWeight) AS StockWeight, 
     SUM(t.SelectedWeight) AS SelectedWeight, 
     SUM(t.BarcodeCount) AS BarcodeCount 
FROM ( SELECT date, 
        SUM(qty) AS StockQuantity, 
        SUM(weight) AS StockWeight, 
        0 AS SelectedWeight, 
        0 AS BarcodeCount 
      FROM stock_list 
      GROUP BY Date 
      UNION ALL 
      SELECT date, 
        0 AS StockQuantity, 
        0 AS StockWeight, 
        SUM(weight) AS SelectedWeight, 
        COUNT(BarCode) AS BarcodeCount 
      FROM selected_items 
      GROUP BY Date 
     ) AS t 
GROUP BY t.Date; 

编辑

我不能对此进行测试,我也不是确保您的确切逻辑的,但你可以使用variables to calculate a running total in MySQL。这应该给如何做的想法:

SELECT Date, 
     StockQuantity, 
     StockWeight, 
     SelectedWeight, 
     BarcodeCount, 
     (@w := @w + StockWeight - SelectedWeight) AS TotalWeight, 
     (@q := @q + StockQuantity - BarcodeCount) AS TotalQuantity 
FROM ( SELECT t.Date, 
        SUM(t.StockQuantity) AS StockQuantity, 
        SUM(t.StockWeight) AS StockWeight, 
        SUM(t.SelectedWeight) AS SelectedWeight, 
        SUM(t.BarcodeCount) AS BarcodeCount 
      FROM ( SELECT date, 
           SUM(qty) AS StockQuantity, 
           SUM(weight) AS StockWeight, 
           0 AS SelectedWeight, 
           0 AS BarcodeCount 
         FROM stock_list 
         GROUP BY Date 
         UNION ALL 
         SELECT date, 
           0 AS StockQuantity, 
           0 AS StockWeight, 
           SUM(weight) AS SelectedWeight, 
           COUNT(BarCode) AS BarcodeCount 
         FROM selected_items 
         GROUP BY Date 
        ) AS t 
      GROUP BY t.Date 
     ) AS t 
     CROSS JOIN (SELECT @w := 0, @q := 0) AS v 
GROUP BY t.Date; 
+0

上一个查询正在处理一个小问题,但这不是。 – AndroidNewBee

+0

你能详细说明“不工作”吗?语法错误?不正确的结果? – GarethD

+0

对不起,它现在正在工作,但有一个小问题,请检查此链接http://i.stack.imgur.com/9gunD.png为20 1月2016你能告诉我为什么我有两个记录那里也可以帮我添加总的项目和当天的总重量。非常感谢你:) – AndroidNewBee

0

您可以使用join。但是,如果两个表中的日期集合不同,那么您需要full outer join。但是这在MySQL中不可用。相反:

select date, sum(qtySum), sum(weightsum1), sum(weightsum2), sum(barcodeCount) 
from ((Select date as Date, Sum(qty) as qtySum, Sum(weight) as weightSum1, 
       NULL as weightsum2, NULL as barcodeCount 
     From stock_list 
     Group by date 
     ) union all 
     (Select date, null, null, Sum(weight), Count(barcode) as barcodeCount 
     From selected_items 
     Group by date 
     ) 
    ) t 
Group by date; 

我不知道你想要的输出如何对应于您所提供的查询。但是,这应该按日期汇总和合并两个表中的数据,以便最终确定查询。

+0

谢谢@GordonLinoff它的工作,我怎么可以把破折号而不是NULL,你能帮我得到总项目和总重量我添加我的表格details.Thank你这么多:) – AndroidNewBee

+0

@AndroidNewBee。 。 。你可能想在应用程序中这样做。短划线是一个字符串,而返回的值是数字。如果你想要一个破折号,你需要小心如何将数字转换为字符串。 –

0

如果两个表中都不存在某些日期,则可以使用FULL JOIN运算符来解决您的任务。

Select ISNULL(A.date, B.date) AS date, 
     A.qtySum, 
     A.weightSum, 
     B.weightSum, 
     B.barcodeCount 
From 
    (Select date as Date, 
      Sum(qty) as qtySum, 
      Sum(weight) as weightSum 
    From stock_list 
    Group by date) as A 
FULL JOIN 
(Select date, 
     Sum(weight) as weightSum, 
     Count(barcode) as barcodeCount 
From selected_items 
Group by date) as B ON A.date = B.date 
相关问题