1、错误SQL :
select o.PK_ORG,o.NAME_TC,o.FK_ORG
from dbo.org o
left join dbo.ORGDUTY od on o.PK_ORG =od.FK_ORG and o.FK_SRCMAIN =od.FK_SRCMAIN
where od.FK_DUTY =1 and o.fk_srcmain=2021
此时,Where 子句中错误放置 " od.FK_DUTY=1 " 会过滤掉 dbo.ORGDUTY 不存在资料的 dbo.ORG,
即把Left Join 该有的作用给抵消了。
2、正确SQL :
select o.PK_ORG,o.NAME_TC,o.FK_ORG
from dbo.org o
left join dbo.ORGDUTY od on o.PK_ORG =od.FK_ORG and o.FK_SRCMAIN =od.FK_SRCMAIN and od.FK_DUTY =1
where o.fk_srcmain=2021