在WHERE子句中对一个语句进行额外过滤

时间:2016-07-28 14:41:29

标签: sql

我有一个包含许多列的大表,特别是我需要使用的三个列:CaseGroup,CaseStatus和CaseDesc。我试图基于打开的CaseStatus,多个CaseGroups来提取数据,然后在一个casegroup上只提供乱丢的CaseDesc。这是我已经拥有的简单部分:

Select  CaseID, CaseStatus, CaseGroup, CaseDesc
FROM  CodeEnforcement
WHERE  CaseStatus = 'Open' AND CaseGroup IN ('ProStd', 'San', 'Tow', 'Weeds')

我想要所有CaseDesc for ProStd,Tow和Weeds。但是,对于San我只想要LIKE '%littering%'。我该如何完成呢?我必须加入两个选择语句吗?

2 个答案:

答案 0 :(得分:0)

您可以使用以下查询:

Select  CaseID, CaseStatus, CaseGroup, CaseDesc
FROM  CodeEnforcement
WHERE  CaseStatus = 'Open' AND 
       (CaseGroup IN ('ProStd', 'Tow', 'Weeds') OR
        (CaseGroup = 'San' AND CaseDesc LIKE '%littering%'))

答案 1 :(得分:0)

您应该在条件中添加or。这样的事情。

Select  CaseID, CaseStatus, CaseGroup, CaseDesc
FROM  CodeEnforcement
WHERE  CaseStatus = 'Open' AND (CaseGroup IN ('ProStd', 'Tow', 'Weeds')
or (CaseGroup = 'San'and CaseDesc like '%littering%')) --make sure you use parentheses