UNION ALL LEFT JOIN和SUM返回不正确的值

时间:2018-12-04 14:13:46

标签: mysql sum left-join union-all

我正在尝试根据库存代码获取每个仓库的库存量,但是总金额未正确返回

你能帮我发现我的错误吗?

产品

+----------+-------------+------------+
| barkod   | stokkodu    |  stokadi   |
+----------+-------------+------------+
| 10057    | U-56544547  |   Çocuk    |
| 10312    | U-56548672  |   Gömlek   |
| 14124    | U-56144681  |   Çanta    |
| 18137    | U-56511551  |   Cüzdan   |
| 17859    | U-16511551  |   Gözlük   |
| 10112    | U-56544672  |   Gömlek   |
| 10124    | U-56544681  |   Çanta    |
| 10137    | U-56545551  |   Cüzdan   |
+----------+-------------+------------+

仓库1

+----------+-------------+------+
| barkod   | stokkodu    | adet |
+----------+-------------+------+
| 10111    | U-56544664  |   27 |
| 10112    | U-56544672  |  141 |
| 10124    | U-56544681  |   84 |
| 10137    | U-56545551  |  254 |
+----------+-------------+------+

仓库2

+----------+-------------+------+
| barkod   | stokkodu    | adet |
+----------+-------------+------+
| 10009    | Unknown     |   61 |
| 10112    | U-56544672  |  131 |
| 10124    | U-56544681  |   85 |
| 10137    | U-56545551  |  254 |
+----------+-------------+------+

我尝试过

SELECT 
   u.stokkodu, 
   products.stokadi,
   SUM(b.adet) AS stokadet,
   SUM(c.adet) AS sayimadet
   FROM (
      SELECT stokkodu FROM wh1 
      UNION ALL
      SELECT stokkodu FROM wh2 WHERE onay = 1 
   ) u   
   LEFT JOIN (
       SELECT 
          stokkodu, 
          adet 
       FROM wh1 
       GROUP BY stokkodu
   ) b ON u.stokkodu = b.stokkodu
   LEFT JOIN (
       SELECT 
          stokkodu, 
          adet 
       FROM wh2 
       WHERE onay = 1 
       GROUP BY stokkodu
   ) c ON u.stokkodu = c.stokkodu
   LEFT JOIN products ON u.stokkodu = products.stokkodu
GROUP BY u.stokkodu

wh1和wh2的商品可能不同

我想要

+----------+-------------+------------+----------+-----------+
| barkod   | stokkodu    |  stokadi   | stokadet | sayimadet |
+----------+-------------+------------+----------+-----------+
| 10111    | U-56544664  |  Gömlek    |     27   |     0     |
| 10112    | U-56544672  |  Gözlük    |     141  |     131   |
| 10124    | U-56544681  |  Ayakkabı  |     84   |     85    |
| 10137    | U-56545551  |  Çanta     |     254  |     254   |
| 10009    | Unknown     |  Unknown   |     0    |     61    |
+----------+-------------+------------+----------+-----------+

谢谢

1 个答案:

答案 0 :(得分:1)

我通过此查询获得了正确的值

SELECT
d.barkod, 
x.stokkodu,
d.stokadi,
IFNULL(b.stokadet,0) AS "stokadet",
IFNULL(c.adet,0) AS "sayimadet"
FROM (  
     SELECT stokkodu FROM wh1 
     UNION ALL 
     SELECT stokkodu FROM wh2 WHERE onay = 1 
) x 
LEFT JOIN ( 
     SELECT 
     stokkodu, 
     IFNULL(SUM(adet),0) as stokadet 
     FROM wh1 
     GROUP BY stokkodu 
) b ON x.stokkodu = b.stokkodu 
LEFT JOIN ( 
     SELECT 
     stokkodu, 
     IFNULL(SUM(adet),0) as sayimadet 
     FROM wh2 
     WHERE onay = 1 
     GROUP BY stokkodu  
) c ON x.stokkodu = c.StokKodu 
LEFT JOIN products d ON x.stokkodu = d.stokkodu 
GROUP BY x.stokkodu