最近项目中出现这样的情况,TestA 表左连接 TestB表,前台查询结果显示出现多条重复记录,经查询数据库得知,TestB中确实有符合条件的记录。
原脚本如下:
select t1.*,t2.*
from TestA t1
left outer join TestB t2
on t1.ch = t2.ch and t2.DateTime >= '20170320000000' and t2.DateTime <= '20170330000000'
where t1.cc='E170328097'
改写后脚本如下:
select t1.*,t2.*
from TestA t1
left outer join TestB t2
on t1.ch = t2.ch
where t1.cc='E170328097'
and t2.id in (select max(id) from TestB t3 where ch = t1.ch and t3.DateTime >= '20170320000000' and t3.DateTime <= '20170330000000' )