碰到一个问题,in+(子查询)+or该如何匹配?

js14982 2016-08-26 05:59:48
测试数据

with a as
(
select 1 a from dual union all
select 2 from dual union all
select 3 from dual union all
select 4 from dual union all
select 5 from dual union all
select 6 from dual union all
select 7 from dual union all
select 8 from dual union all
select 9 from dual
), b as(
select 1 a from dual union all
select 2 from dual union all
select 3 from dual
)
select * from a
where (a in (select a from b)
or a=5 or a=7)
;

现在由于某些特殊原因,不能用in+子查询这种写法。
尝试考虑把子查询(select a from b)置为b表和a表关联,可是后面的or a=5 or a=7如何处理?

数据量较大,不要考虑排列重复再去重distinct的写法。
即select distinct a.a from a,
(select a from b) b
where (a.a=b.a or a.a=5 or a.a=7) 这种写法。
...全文
258 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
不能用left join,用了表示a表的数据都要,LZ的意思并是不这样 不用in、exists、union all,想不出来其他更好的办法
卖水果的net 2016-08-29
  • 打赏
  • 举报
回复
引用 5 楼 js14982 的回复:
这儿应该也是有点问题,不能用left join直接用join,当时考虑过这种写法,不过原语句还有group by,就是想问下有没不用union all的写法。(不考虑exists)。
有 group by ,可以多写一层 select ,把 group by 放在内层,外面再使用 left join 这个写法可以接受吗?
js14982 2016-08-29
  • 打赏
  • 举报
回复
引用 4 楼 wmxcn2000 的回复:
-- 可以考虑这样写
select * from a
where a left join b on a.a = b.a 
union all
select * from a where a=5 or a=7)
这儿应该也是有点问题,不能用left join直接用join,当时考虑过这种写法,不过原语句还有group by,就是想问下有没不用union all的写法。(不考虑exists)。
卖水果的net 2016-08-29
  • 打赏
  • 举报
回复
-- 可以考虑这样写
select * from a
where a left join b on a.a = b.a 
union all
select * from a where a=5 or a=7)
cnwz 2016-08-28
  • 打赏
  • 举报
回复
不知道你要干嘛,貌似exists性能会好些
select * from a
where (exists (select 1 from b where a.a=b.a) or a=5 or a=7)
goodgo032318 2016-08-27
  • 打赏
  • 举报
回复
没看懂你要干啥
卖水果的net 2016-08-26
  • 打赏
  • 举报
回复
两部分结果,union all

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧