SQL - 仅用于连接5个表的当前表的sum字段

时间:2013-02-13 03:36:36

标签: mysql sql

**************已解决,但欢迎更好的答案! :) **************

我想从表sc000中得到(sa003 * sa004)的总和,在此之前我必须检查其他表上是否存在的项目。图像(某些表具有相同的列格式):

enter image description here

我使用'IN':

... WHERE s.sa002 IN (select b.b005 from b000 b where year(b.b001) = 2013 and month(b.b001) = 1)

但它需要一个漫长的过程,所以我尝试使用'LEFT JOIN':

 SELECT s.sa002, Sum(s.SA003) AS qty, Sum(s.SA003*s.SA004) AS jumlah
 FROM sc000 s LEFT JOIN
 (select bs.b005 from b000 bs where year(bs.b001) = 2013 and month(bs.b001) = 1) b
   on s.sa002 = b.b005 LEFT JOIN
 (SELECT ps.p005 from p000 ps where year(ps.p001) = 2013 and month(ps.p001) = 1 ) p
   on s.sa002 = p.p005 LEFT JOIN
 (SELECT js.b005 from j000 js where year(js.b001) = 2013 and month(js.b001) = 1 ) j
   on s.sa002 = j.b005 LEFT JOIN
 (SELECT rs.b005 from Rev00 rs where year(rs.b001) = 2013 and month(rs.b001) = 1) rv
   on s.sa002 = rv.b005 LEFt JOIN
 (SELECT es.b005 FROM R000 es where year(es.b001) = 2013 and month(es.b001) = 1) re
   on s.sa002 = re.b005
 where year(s.sa005) = 2013 and month(s.sa005) = 1
 and (b.b005 is not null or p.p005 is not null or rv.b005 is not null or j.b005 is not null or re.b005 is not null)
 GROUP BY s.sa002  

结果成倍增加。有帮助吗? :(

更新: 我想要这样的结果(代码,数量从sc002,数量*价格):

enter image description here

^没有检查其他表。

然后,使用左连接,结果如下:

enter image description here

2 个答案:

答案 0 :(得分:0)

您的一个(或某些)联接不会以一对一的关系进行链接。最简单的检查方法是删除group by和sum()语句,并在b,p,j,rv和re表中包含一些列,如下所示:

SELECT s.sa002, s.SA003 AS qty, s.SA003*s.SA004 AS  jumlah, g.gName as gNames, 
 p.p005, j.b005, rv.b005, re.b005
 FROM sc000 s LEFT JOIN
 (select bs.b005 from b000 bs where year(bs.b001) = 2013 and month(bs.b001) = 1) b
   on s.sa002 = b.b005 LEFT JOIN
 (SELECT ps.p005 from p000 ps where year(ps.p001) = 2013 and month(ps.p001) = 1 ) p
   on s.sa002 = p.p005 LEFT JOIN
 (SELECT js.b005 from j000 js where year(js.b001) = 2013 and month(js.b001) = 1 ) j
   on s.sa002 = j.b005 LEFT JOIN
 (SELECT rs.b005 from Rev00 rs where year(rs.b001) = 2013 and month(rs.b001) = 1) rv
   on s.sa002 = rv.b005 LEFt JOIN
 (SELECT es.b005 FROM R000 es where year(es.b001) = 2013 and month(es.b001) = 1) re
   on s.sa002 = re.b005
 where year(s.sa005) = 2013 and month(s.sa005) = 1
 and (b.b005 is not null or p.p005 is not null or rv.b005 is not null or j.b005 is not null or re.b005 is not null)
ORDER BY s.sa002

或者,甚至更好,

 SELECT s.sa002, count(*)
 FROM sc000 s LEFT JOIN
 (select bs.b005 from b000 bs where year(bs.b001) = 2013 and month(bs.b001) = 1) b
   on s.sa002 = b.b005 LEFT JOIN
 (SELECT ps.p005 from p000 ps where year(ps.p001) = 2013 and month(ps.p001) = 1 ) p
   on s.sa002 = p.p005 LEFT JOIN
 (SELECT js.b005 from j000 js where year(js.b001) = 2013 and month(js.b001) = 1 ) j
   on s.sa002 = j.b005 LEFT JOIN
 (SELECT rs.b005 from Rev00 rs where year(rs.b001) = 2013 and month(rs.b001) = 1) rv
   on s.sa002 = rv.b005 LEFt JOIN
 (SELECT es.b005 FROM R000 es where year(es.b001) = 2013 and month(es.b001) = 1) re
   on s.sa002 = re.b005
 where year(s.sa005) = 2013 and month(s.sa005) = 1
 and (b.b005 is not null or p.p005 is not null or rv.b005 is not null or j.b005 is not null or re.b005 is not null)
 GROUP BY s.sa002  
 HAVING count(*) > 1

答案 1 :(得分:0)

感谢所有看到或试图帮助解决此问题的人。 :d

我已经找到了查询!

我正在使用UNION加入5个表,并得到了我想要的答案。