oracle a表 左连接 b表,b表中存在多条记录,只返回一条记录的处
发布时间:2020-12-12 15:07:17 所属栏目:百科 来源:网络整理
导读:最近项目中出现这样的情况,TestA 表左连接 TestB表,前台查询结果显示出现多条重复记录,经查询数据库得知,TestB中确实有符合条件的记录。 原脚本如下: select t1.*,t2.*from TestA t1 left outer join TestB t2 on t1.ch = t2.ch and t2.DateTime = '201
最近项目中出现这样的情况,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' )
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |