SQL where子句中的多重搜索

时间:2018-09-13 15:56:47

标签: sql-server wildcard where-clause

在where子句中使用多重搜索需要帮助:

尝试编写一个查询,该查询将返回“已关闭完成”,“已关闭未完成”,“已关闭跳过”中的票证,并排除列中包含“项目”的行,但是如果包含投影仪,ms项目,项目,则不应排除该查询2016,投影仪

这是我到目前为止的查询:

请让我知道我在做什么错

Select 
CONCAT(YEAR(a.[closed_at]),'-',DATENAME(MONTH,a.[closed_at])) as [Year_Month],
a.[Number],
a.[closed_at],
b.[Region], 
b.[Tower_Name],
a.[Short_description],
a.[description]
from [dbo].[sc_task_master_data] a
right join [dbo].[Master_Assignee_list] b
on a.[assignment_group]=b.Assignee_Group_Name
where a.[state] IN ('Closed Complete','Closed Incomplete','Closed Skipped')
and a.[short_description] not like '%project%'
and a.[short_description] like '%project 2%'
and a.[short_description] like '%MS Project%'
and a.[short_description] like '%PROJECT 20%'
and a.[short_description] like '%project installation%'
and a.[short_description] like '%Microsoft Project%'
and a.[short_description] like '%Projector%'
and a.[number] in ('SCTASK0050503',  'SCTASK0050510','SCTASK0051162')

谢谢!

1 个答案:

答案 0 :(得分:1)

您的WHERE子句仅包含AND,这意味着之前和之后的条件必须匹配。

您可以修改语句以包含括号,该括号可以有效地将子句组合在一起以具有单个逻辑结果,例如:

where a.[state] IN ('Closed Complete','Closed Incomplete','Closed Skipped')
and (a.[short_description] not like '%project%'
or a.[short_description] like '%project 2%'
or a.[short_description] like '%MS Project%'
or a.[short_description] like '%PROJECT 20%'
or a.[short_description] like '%project installation%'
or a.[short_description] like '%Microsoft Project%'
or a.[short_description] like '%Projector%')
and a.[number] in ('SCTASK0050503',  'SCTASK0050510','SCTASK0051162')

请注意第二行和倒数第二行中的括号,这些括号将这些子句分组在一起,以便如果其中的 any 为true,则为true。