SQL where 子句中的多重搜索

SQL multiple search in where clause

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

尝试编写一个查询,该查询将 'Closed Complete'、'Closed Incomplete'、'Closed Skipped' 中的票 return 并排除列中包含 Project 的行,但它不应该如果包含 projector、ms project、project 2016、projector

,则排除

这是我目前的查询:

请让我知道我做错了什么

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')

谢谢!

您的 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')

请注意第二行和倒数第二行中的括号,它将这些子句组合在一起,以便如果其中 任何 个为真,则它为真。