[SQL ] 这样的情况能否有更快速的写法

楼主: aoksc (重出江湖)   2015-11-16 22:11:37
(针对 SQL 语言的问题,用这个标题。请用 Ctrl+Y 砍掉这行)
数据库名称:SQL SERVER
数据库版本:2008、2012
内容/问题描述:
请问各位
如果有某个查询其实条件都差不多
但因为有用到OR的情况所以我用union来合并查询结果代替OR
像是这样
select * from xxx where aaa=1 and bbb='OOXX'
union
select * from xxx where aaa=1 and ddd='2015/11/11'
union
select * from xxx where aaa=1 and ccc=9527
想请问一下
是否有类似先取得select * from xxx where aaa=1的结果暂存
再去套bbb、ccc、ddd的查询条件是不是会更快
请问有这种写法吗?
谢谢
作者: bohei (run and fall)   2015-11-16 23:06:00
有啊 你自己建一个temp table @@
作者: a926 (Aaron)   2015-11-17 09:52:00
可以考虑用CTE
作者: clonk (咚)   2015-11-17 11:53:00
不能直接OR吗?aaa=1 and (bbb='2' or ddd='3' or ccc='4')
作者: JeremyJoung (J.J.)   2015-11-17 12:06:00
楼上的方法比较正确 而且原始方法会导致 b=2&&d=3时资料行重复出现的可能
作者: likesp999 (大肠杆菌)   2015-11-17 12:17:00
原po有下union但没下all因该就不会重复吧!

Links booklink

Contact Us: admin [ a t ] ucptt.com