left join 左邊有數據,右邊無數據


 
主要是andwhere的區別:
 
原Sql:
select a.ID as reqid,(select top 1 Convert(nvarchar(50),jhdatez,111) from CS_OrderPro
where Codeno=a.InvNo and iProductId =a.iProductId)as 'jhdate',
(select top 1 Deptname from UserInfo left join Department on  UserInfo.deptid=Department.Deptid where UserInfo.userid=AuditUserId) as deptname,
(select isnull(SUM(iNum),0) from CS_OrderPro where CS_OrderPro.zid=a.ID and  CS_OrderPro.codeno=a.Invno) as cgnum,
(select top 1 InvNO from CS_OrderPro where CS_OrderPro.zid=a.ID and  CS_OrderPro.codeno=a.Invno) as cginvno,
(select isnull(SUM(CS_InvRecBill.iNum),0) from CS_InvRecBill
left join  CS_OrderPro on CS_InvRecBill.zid=CS_OrderPro.ID
where CS_OrderPro.zid=a.ID and CS_OrderPro.codeno=a.Invno) as rknum,*,
isnull((select sum(pronum) from cs_stock where proid=a.iproductid),0) as kcnum,
comoditiestype from CS_OrderRequise a left join CS_OrderRequiseList c on c.OutCode=a.InvNO
left join B_Products b on b.id=a.iProductID 
left join ComoditiesType on ComoditiesType.typeid=b.typeid    where 1=1 and c.isend=1 and ComoditiesType.typeid<>336

左連接查詢后,最后的where語句過濾了本該顯示的數據。

修改過的Sql:

select a.ID as reqid,(select top 1 Convert(nvarchar(50),jhdatez,111) from CS_OrderPro
where Codeno=a.InvNo and iProductId =a.iProductId)as 'jhdate',
(select top 1 Deptname from UserInfo left join Department on  UserInfo.deptid=Department.Deptid where UserInfo.userid=AuditUserId) as deptname,
(select isnull(SUM(iNum),0) from CS_OrderPro where CS_OrderPro.zid=a.ID and  CS_OrderPro.codeno=a.Invno) as cgnum,
(select top 1 InvNO from CS_OrderPro where CS_OrderPro.zid=a.ID and  CS_OrderPro.codeno=a.Invno) as cginvno,
(select isnull(SUM(CS_InvRecBill.iNum),0) from CS_InvRecBill
left join  CS_OrderPro on CS_InvRecBill.zid=CS_OrderPro.ID
where CS_OrderPro.zid=a.ID and CS_OrderPro.codeno=a.Invno) as rknum,*,
isnull((select sum(pronum) from cs_stock where proid=a.iproductid),0) as kcnum,
comoditiestype from CS_OrderRequise a left join CS_OrderRequiseList c on c.OutCode=a.InvNO
left join B_Products b on b.id=a.iProductID
left join ComoditiesType on ComoditiesType.typeid=b.typeid AND ComoditiesType.typeid<>336   where 1=1 and c.isend=1 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM