首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求一条多表关联查询SQL语句解决思路

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

求一条多表关联查询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

热点排行