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

求一条多表关联语句
本人对sql语句不熟,请大家帮忙写一下下面的语句

有三句语句分别为:

第一句:
 select a.bm_gsjg, a.bm_ck, a.bm_sp, a.mc, a.dj_ls,a.sl_qm from v_zb_kc_draft a left join t_bm_splimit b on a.bm_gsjg = b.bm_gsjg and a.bm_ck = b.bm_ck and a.bm_sp = b.bm_sp and is_kc = 1 Where 1 = 1 And isnull(a.bm_gsjg,'') like 'CYZ' and sl_qm > 0 and a.bm_sp like 'DPZBZD001%' ORDER BY a.bm_gsjg,a.bm_ck,a.bm_sp 

第二名:
 select bm_gsjg, bm_ck, bm_sp, mc, dj_ls, sl_qm from v_zb_kc Where is_kc = 1 And isnull(bm_gsjg,'') like 'CYZ' And isnull(bm_sp,'') like 'DPZBZD001%' ORDER BY bm_gsjg,bm_ck,bm_sp 

第三句:
SELECT t_bm_spdj.bm_sp, t_bm_spdj.dj_ls, t_bm_sp.dj_min FROM t_bm_spdj, t_bm_sp WHERE t_bm_spdj.bm_sp = t_bm_sp.bm and ( t_bm_spdj.bm_sp = 'DPZBZD001' ) and bm_gsjg = '' and bm_ck = '' and bzgg = 0

第一句与第二句中的bm_gsjg,bm_ck,bm_sp,mc为相同值
第一句、第二句、第三句中的bm_sp为相同值

如何关联这三条语句,一次得到如下的值
第一句中的sl_qm
第二句中的sl_qm
第三句中的t_bm_sp.dj_min



------解决方案--------------------
SQL code

select A.sl_qm ,B.sl_qm ,C.dj_min
(
select a.bm_gsjg, a.bm_ck, a.bm_sp, a.mc, a.dj_ls,a.sl_qm  
from  v_zb_kc_draft a 
left join  t_bm_splimit b  on a.bm_gsjg = b.bm_gsjg and a.bm_ck = b.bm_ck and a.bm_sp = b.bm_sp and is_kc = 1  
Where 1 = 1 And isnull(a.bm_gsjg,'') like 'CYZ' and sl_qm > 0 and a.bm_sp like 'DPZBZD001%' 
--ORDER BY a.bm_gsjg,a.bm_ck,a.bm_sp 
) A
left join 
( 
select  bm_gsjg,  bm_ck, bm_sp,    mc, dj_ls,    sl_qm  
from  v_zb_kc  
Where is_kc = 1        And isnull(bm_gsjg,'') like 'CYZ'      And isnull(bm_sp,'') like 'DPZBZD001%' 
--ORDER BY bm_gsjg,bm_ck,bm_sp 
) B on A.bm_gsjg=bm_gsjg. and A.bm_ck=B.bm_ck and A.bm_sp=B.bm_sp and A.mc=B.mc
(
SELECT t_bm_spdj.bm_sp, t_bm_spdj.dj_ls, t_bm_sp.dj_min FROM t_bm_spdj, t_bm_sp  
WHERE t_bm_spdj.bm_sp = t_bm_sp.bm and ( t_bm_spdj.bm_sp = 'DPZBZD001' )  and bm_gsjg = '' and bm_ck = '' and bzgg = 0
) C on A.bm_sp=C.bm_sp
ORDER BY a.bm_gsjg,a.bm_ck,a.bm_sp

------解决方案--------------------
SQL code
select A.sl_qm ,B.sl_qm ,C.dj_min
(
select a.bm_gsjg, a.bm_ck, a.bm_sp, a.mc, a.dj_ls,a.sl_qm  
from  v_zb_kc_draft a 
left join  t_bm_splimit b  on a.bm_gsjg = b.bm_gsjg and a.bm_ck = b.bm_ck and a.bm_sp = b.bm_sp and is_kc = 1  
Where 1 = 1 And isnull(a.bm_gsjg,'') like 'CYZ' and sl_qm > 0 and a.bm_sp like 'DPZBZD001%' 
--ORDER BY a.bm_gsjg,a.bm_ck,a.bm_sp 
) A
left join 
( 
select  bm_gsjg,  bm_ck, bm_sp,    mc, dj_ls,    sl_qm  
from  v_zb_kc  
Where is_kc = 1        And isnull(bm_gsjg,'') like 'CYZ'      And isnull(bm_sp,'') like 'DPZBZD001%' 
--ORDER BY bm_gsjg,bm_ck,bm_sp 
) B on A.bm_gsjg=bm_gsjg. and A.bm_ck=B.bm_ck and A.bm_sp=B.bm_sp and A.mc=B.mc
left join 
(
SELECT t_bm_spdj.bm_sp, t_bm_spdj.dj_ls, t_bm_sp.dj_min FROM t_bm_spdj, t_bm_sp  
WHERE t_bm_spdj.bm_sp = t_bm_sp.bm and ( t_bm_spdj.bm_sp = 'DPZBZD001' )  and bm_gsjg = '' and bm_ck = '' and bzgg = 0
) C on A.bm_sp=C.bm_sp
ORDER BY a.bm_gsjg,a.bm_ck,a.bm_sp

------解决方案--------------------
select 
a.sl_qm,b.sl_qm,c.dj_min
from
(select a.bm_gsjg, a.bm_ck, a.bm_sp, a.mc, a.dj_ls,a.sl_qm from v_zb_kc_draft a left join t_bm_splimit b on a.bm_gsjg = b.bm_gsjg and a.bm_ck = b.bm_ck and a.bm_sp = b.bm_sp and is_kc = 1 Where 1 = 1 And isnull(a.bm_gsjg,'') like 'CYZ' and sl_qm > 0 and a.bm_sp like 'DPZBZD001%')a
left join 
(select bm_gsjg, bm_ck, bm_sp, mc, dj_ls, sl_qm from v_zb_kc Where is_kc = 1 And isnull(bm_gsjg,'') like 'CYZ' And isnull(bm_sp,'') like 'DPZBZD001%')b
on
a.bm_gsjg=b.bm_gsjg,a.bm_ck=b.bm_ck,a.bm_sp=b.bm_sp,a.mc=.mc
left join
(SELECT t_bm_spdj.bm_sp, t_bm_spdj.dj_ls, t_bm_sp.dj_min FROM t_bm_spdj, t_bm_sp WHERE t_bm_spdj.bm_sp = t_bm_sp.bm and ( t_bm_spdj.bm_sp = 'DPZBZD001' )