如何将两个表与另一个表

时间:2016-05-11 11:16:50

标签: sql

我有三个表作为pf,pm和s.i已经成功地加入了表pf和pm.but但是当我尝试将表连接到这两个表时,我正在努力。

表pf包含 - 样式,颜色,米,码,千,grn_no和tag_gen

表pm包含 - date,customer_id,gp_no

我使用pm.go_no和pf.grn_no.it加入这两个表。 现在我想加入s表。

表包含 - customer_id,style,color和unit_qty

所以我想要做的是我想获得与pf表样式,颜色和pm表customer_id相匹配的Unit_qty。

这是我的代码

SELECT pm.date, pm.customer, pf.style, pf.color, s.unit_qty, COUNT(pf.roll_no) AS Roll_QTY, SUM(pf.yard) AS Yard_QTY, SUM(pf.meter) AS Meter_QTY, SUM(pf.kilo) AS Kilo_QTY, SUM( case when pf.aql_status = 0  then pf.meter else 0 end ) FB_Not_Passed_Qty, SUM( case when pf.aql_status = 1 and  pf.tag_gen = 0  then pf.meter else 0 end ) Not_Planned_Qty 
FROM packinglists_main pm, packinglists_fabrics_items pf, style_data s 
WHERE pf.grn_no= pm.gp_no[ AND pm.date BETWEEN {DateR,RANGE1} AND {DateR,RANGE2}][ AND s.style = pf.style][ AND s.color = pf.color][ AND s.customer_id = pm.customer_id] 
GROUP BY pm.customer, pf.style, pf.color

1 个答案:

答案 0 :(得分:0)

这将返回三个表中的所有列。做你想做的任何事情

SELECT * from pf 
INNER JOIN pm on pf.grn_no = pm.gp_no 
INNER JOIN s on s.style = pf.style
       AND s.Color = pf.Color 
       AND s.CustomerID = pm.CustomerID