三个表 父 -子-子 a b c
a与b 1对多, b与c 1对多
类似 担保内容表 -担保公司表-担保公司合同图片表
一条担保信息,包括多个担保公司,每个担保公司又包括多个合同图片
有时候数据删除是伪删除. 在联查图片数据的时候,需要每个表的数据IsValid=1
在c 表图片表 那条担保合同的图片伪删除的状态下
--错误--数据空了 select * from a left join b on a.aID=b.aID left join c on b.bID = c.bID where b.IsValid = 1 and a.IsValid = 1 and c.IsValid=1 and a.aid=79
此时 isvalid =1 条件写在 where 之后, left 完之后 进行过滤,结果就是所有值为空了. 因为 c表的图片 isvalid为0.
而此时修改下写法,把isvalid 的条件放到 join on on 后边,则能把a表,b表的内容展示出来,c表的内容为null
也吻合了left join 的定义了.正确写法
select * from a left join b on a.aID=b.aID and b.IsValid = 1 and a.IsValid = 1 left join c on b.bID = c.bID and c.IsValid=1 where a.aid=79
他记
合并
UPDATE [rzdb].[dbo].[O_Lo5n5yment] SET IsValid = 0 WHERE Loa5mentID not in ( select max(LoanRepayme5) FROM [rzdb].[dbo].[OpsR5_Loa5yment] group by Loa5ID)