是否有一种更简单的方法可以使用'like'

时间:2016-09-20 12:47:04

标签: sql union sql-like

我可以使用Union创建查询。但是我想知道是否有更简单的方法来创建相同的结果?我尝试过使用in ('abc%','def%')like ('abc%','def%'),但是在逗号分隔括号中的两个单词的逗号附近出现语法错误。

这是我到目前为止所拥有的......

select distinct a.case,a.person,a.personid,b.plans,a.code,a.action,cast(a.datea as date) as datea,cast(a.dateb as date) as dateb,a.updatecode
from table(nolock) a

inner join table(nolock) b on a.column=b.column

where updatecode = 'invalid'

and code like ('pr%') and datea >= '2016-09-16' and len (b.plans) = 16

union

select distinct a.case,a.person,a.personid,b.plans,a.code,a.action,cast(a.datea as date) as datea,cast(a.dateb as date) as dateb,a.updatecode

from table(nolock) a

inner join table(nolock) b on a.column=b.column

where updatecode = 'invalid'

and code like ('sea%') and datea >= '2016-09-16' and len (b.plans) = 16

3 个答案:

答案 0 :(得分:1)

您可以使用:

(code like 'pr%' OR code like 'sea%')

答案 1 :(得分:1)

我不相信这是一个很好的,看起来很干净的解决方案,但您可以使用like对多个or个查询进行排序,如:

 select * from config.devices where displayname like 'Im%' or displayname like 'In%'

这将返回以“Im”或“In”开头的所有结果。

答案 2 :(得分:1)

试试这个:

select distinct a.case,a.person,a.personid,b.plans,a.code,a.action,cast(a.datea as date) as datea,cast(a.dateb as date) as dateb,a.updatecode
from table(nolock) a
inner join table(nolock) b on a.column=b.column
where updatecode = 'invalid'
and (code like ('pr%') OR code like ('sea%'))
and datea >= '2016-09-16' 
and len (b.plans) = 16