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 

 

转载于:https://www.cnblogs.com/ywkcode/p/11491757.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值