我正在尝试创建一个搜索数据库并根据特定ID
显示结果的过滤器WHERE DATEDIFF(YEAR, A.AcquiredDate, GetDate()) >=5 AND A.AssetTypeID = 7 AND A.CategoryID = 5
WHERE DATEDIFF(YEAR, A.AcquiredDate, GetDate()) >=5 AND A.AssetTypeID = 7 AND A.CategoryID = 5 AND A.CategoryID = 4
添加代码AND A.CategoryID = 4
的第二部分后,查询会吐出0条记录。
如何同时过滤A.CategoryID = 5 AND A.CategoryID = 4
?
答案 0 :(得分:3)
像这样更改Where condition
。使用OR
运算符或IN
条款代替AND
如果第一个条件和第二个条件都为真,AND
运算符会显示一条记录。
如果第一个条件或第二个条件为真,OR
运算符会显示记录。
WHERE DATEDIFF(YEAR, A.AcquiredDate, GetDate()) >=5
AND A.AssetTypeID = 7
AND (A.CategoryID = 5 or A.CategoryID = 4)
OR
WHERE DATEDIFF(YEAR, A.AcquiredDate, GetDate()) >=5
AND A.AssetTypeID = 7
AND A.CategoryID IN (5,4)
答案 1 :(得分:0)
您可以使用OR或IN。所以:
WHERE DATEDIFF(YEAR, A.AcquiredDate, GetDate()) >=5 AND A.AssetTypeID = 7 AND (A.CategoryID = 4 OR A.CategoryID = 5)
或者:
WHERE DATEDIFF(YEAR, A.AcquiredDate, GetDate()) >=5 AND A.AssetTypeID = 7 AND A.CategoryID IN (4, 5)