日期:2014-05-18  浏览次数:20782 次

求一条多表关联查询SQL语句
select o.order_list_id,z.size_name,c.color_name,y.style_name,
sum(o.num) as totalNum

from t_order_list o,t_size z,t_color c,t_style y, 
t_order_style os,t_order_form f
where  

o.size_id = z.size_id and 
o.color_id = c.color_id and o.order_style_id = os.order_style_id and  
os.style_id = y.style_id and f.order_form_id = os.order_form_id and
os.order_form_id = f.order_form_id and 
f.order_form_id = 18

--and left outer join t_work_list w on w.order_list_id = o.order_list_id
group by o.order_list_id,z.size_name,c.color_name,y.style_name
正如红色字体部分,我想把多表联合查询出来的结果再和一个表左外联。这样怎么解决这个问题。


------解决方案--------------------
select * from (select o.order_list_id,z.size_name,c.color_name,y.style_name, 
sum(o.num) as totalNum 

from t_order_list o,t_size z,t_color c,t_style y,
t_order_style os,t_order_form f 
where

o.size_id = z.size_id and
o.color_id = c.color_id and o.order_style_id = os.order_style_id and
os.style_id = y.style_id and f.order_form_id = os.order_form_id and 
os.order_form_id = f.order_form_id and
f.order_form_id = 18 ) a left outer join t_work_list w on w.order_list_id = a.order_list_id
------解决方案--------------------
select * from (
select o.order_list_id,z.size_name,c.color_name,y.style_name, 
sum(o.num) as totalNum 
from t_order_list o,t_size z,t_color c,t_style y,
t_order_style os,t_order_form f 
where o.size_id = z.size_id and
o.color_id = c.color_id and o.order_style_id = os.order_style_id and
os.style_id = y.style_id and f.order_form_id = os.order_form_id and 
os.order_form_id = f.order_form_id and
f.order_form_id = 18 ) TT 
left outer join t_work_list w on w.order_list_id = a.order_list_id
------解决方案--------------------
前面全部使用 JOIN ON 联接,最后用LEFT OUTER JOIN