mysql sum() 和双重结果使用多个连接

Posted

技术标签:

【中文标题】mysql sum() 和双重结果使用多个连接【英文标题】:mysql sum() and double result Using Multiple Joins 【发布时间】:2021-01-08 15:43:52 【问题描述】:

我有表 tb_orders

id_order lot rekanan no_order pec
5555 1 GOZNAK 390 y
6666 1 KOMSCO 391 y

我还有一张桌子 tb_pengambilans_detail

id id_order tanggal jumlah
137 5555 2021-01-05 09:36:16 100000
136 6666 2021-01-05 09:35:57 100000
94 6666 2020-12-15 15:00:47 100000
138 5555 2021-01-05 09:37:51 100000

表格 tb_lini_pengambilans_detail

id id_order tanggal deret5
38 6666 2020-12-08 09:26:36 30000
39 5555 2020-12-08 09:31:49 10000
40 6666 2020-12-14 07:55:36 80000
41 6666 2020-12-14 07:57:34 20000

表 tb_produksi_cutpacks_detail

id tanggal id_order hcts unfit
30 2021-01-04 13:02:10 5555 5000 0
32 2021-01-04 14:10:05 5555 0 10000
34 2021-01-04 15:11:57 5555 0 2000
35 2021-01-04 15:14:10 6666 9000 0

表 tb_lini_produksi_cutpacks_detail

id tanggal id_order xbaik xderet5
16 2021-01-04 14:52:38 5555 4000 1000
18 2021-01-04 14:53:21 6666 8000 0
19 2021-01-04 14:53:36 6666 0 2000
21 2021-01-04 14:57:41 5555 0 15000

我分别做了,所有结果都是正确的,不重复

SELECT a.rekanan,
    SUM(COALESCE(b.jumlah,0)) AS `pengambilan`
 FROM tb_orders a
LEFT JOIN tb_pengambilans_detail b USING(id_order)
WHERE a.pec='y'
GROUP BY a.rekanan

结果

rekanan pengambilan
GOZNAX 200000
KOMSCO 200000
SELECT a.rekanan,
  SUM(COALESCE(c.unfit + c.hcts,0)) AS prod_hcts
FROM tb_orders a
LEFT JOIN tb_produksi_cutpacks_detail c USING(id_order)
WHERE a.pec='y'
GROUP BY a.rekanan

结果

rekanan prod_hcts
GOZNAX 17000
KOMSCO 9000
SELECT a.rekanan,
  SUM(COALESCE(d.deret5 ,0)) AS `pengambilanlini`
FROM tb_orders a
LEFT JOIN tb_lini_pengambilans_detail d USING(id_order)
LEFT JOIN tb_lini_produksi_cutpacks_detail e USING(id_order)
WHERE a.pec='y'
GROUP BY a.rekanan

结果

rekanan pengambilanlini
GOZNAX 20000
KOMSCO 260000

最后

SELECT a.rekanan,
  SUM(COALESCE(e.xbaik + e.xderet5 ,0)) AS prod_hctslini
FROM tb_orders a
LEFT JOIN tb_lini_produksi_cutpacks_detail e USING(id_order)
WHERE a.pec='y'
GROUP BY a.rekanan

结果

rekanan prod_hctslini
GOZNAX 20000
KOMSCO 10000

在我确定一切之后,我加入了所有表格,结果都是双倍的......

SELECT a.rekanan,
SUM(COALESCE(b.jumlah,0)) AS `pengambilan`,
SUM(COALESCE(c.unfit + c.hcts,0)) AS prod_hcts,
SUM(COALESCE(d.deret5 ,0)) AS `pengambilanlini`,
SUM(COALESCE(e.xbaik + e.xderet5 ,0)) AS prod_hctslini
FROM tb_orders a
LEFT JOIN tb_pengambilans_detail b USING(id_order)
LEFT JOIN tb_produksi_cutpacks_detail c USING(id_order)
LEFT JOIN tb_lini_pengambilans_detail d USING(id_order)
LEFT JOIN tb_lini_produksi_cutpacks_detail e USING(id_order)
WHERE a.pec='y'
GROUP BY a.rekanan

结果

rekanan pengambilan prod_hcts pengambilanlini prod_hctslini
GOZNAX 1200000 68000 120000 120000
KOMSCO 1200000 108000 520000 60000

有什么办法让SQL结果不是double.. 请告诉我,以便解决问题.. 非常感谢

【问题讨论】:

【参考方案1】:

----实际的单SQL解决方案是---------------

 SELECT a.rekanan,
SUM(COALESCE(b.jumlah,0)) AS `pengambilan`,
SUM(COALESCE(c.unfit + c.hcts,0)) AS prod_hcts,
SUM(COALESCE(d.deret5 ,0)) AS `pengambilanlini`,
SUM(COALESCE(e.xbaik + e.xderet5 ,0)) AS prod_hctslini
FROM tb_orders a
LEFT JOIN (select id_order ,SUM(jumlah) jumlah from  tb_pengambilans_detail group by id_order)  b USING(id_order)
LEFT JOIN (select id_order ,SUM(unfit) unfit ,SUM(hcts) hcts  from  tb_produksi_cutpacks_detail group by id_order)  c  USING(id_order)
LEFT JOIN  (select id_order ,SUM(deret5) deret5  from  tb_lini_pengambilans_detail group by id_order) d USING(id_order)
LEFT JOIN  (select id_order ,SUM(xbaik) xbaik , sum(xderet5) xderet5  from  tb_lini_produksi_cutpacks_detail group by id_order) e USING(id_order)
WHERE a.pec='y'
GROUP BY a.rekanan

【讨论】:

非常感谢您的回答..我的问题终于解决了..它的工作..问候 对不起,我从来没有投票过……我该如何投票? 没问题...只需点击答案的左侧向上箭头 哦好吧..对不起..我投给你了..再次感谢你

以上是关于mysql sum() 和双重结果使用多个连接的主要内容,如果未能解决你的问题,请参考以下文章

MYSQL 更新使用 sum() 结果跨多个表

MySQL sum 和 distinct 在具有多个连接的另一列上

将 concat 和 sum 与左连接 mysql 分组

多个mysql表列的Sum,Subtract和Join

MySql:查询优化(多个左连接)

从 MySQL 中具有不同列的表的多个连接结果中删除重复项