TSQL Where子句满足多个条件

时间:2021-04-24 23:10:14

I have a basic select query that is pulling in the results of a queue to our page. I am trying to add in some logic to the where clause that prevents records from showing when they contain a specific set of values.

我有一个基本的select查询,它将队列的结果拖到页面。我试图在where子句中添加一些逻辑,该子句阻止记录显示它们何时包含一组特定的值。

WHERE (
        (L.requestType <> 'Check')
        AND (L.requestStatus <> 'New Request')
        AND (DATEDIFF(hour, CAST(L.requestDate AS DATE), GETDATE()) between 0 and 48)
      )

In the example above, I don't want to include records that have all three data points.

在上面的示例中,我不想包含所有三个数据点的记录。

When doing some testing, it is excluding All "Checks" for example even though those records do not also meet the other two pieces of criteria.

当执行一些测试时,它排除了所有的“检查”,例如,即使那些记录也不满足另外两个标准。

How can I go about making my where clause meet all criteria and not do what appears to be an or?

我如何使where子句符合所有标准,而不做看起来是or的事情?

1 个解决方案

#1


4  

You need

你需要

WHERE NOT (L.requestType = 'Check'
           AND L.requestStatus = 'New Request'
           AND (DATEDIFF(hour, CAST(L.requestDate AS DATE), GETDATE()) between 0 and 48
          )

which is equivalent to

相当于

WHERE (L.requestType <> 'Check'
       OR L.requestStatus <> 'New Request'
       OR DATEDIFF(hour, CAST(L.requestDate AS DATE), GETDATE()) not between 0 and 48
       )

#1


4  

You need

你需要

WHERE NOT (L.requestType = 'Check'
           AND L.requestStatus = 'New Request'
           AND (DATEDIFF(hour, CAST(L.requestDate AS DATE), GETDATE()) between 0 and 48
          )

which is equivalent to

相当于

WHERE (L.requestType <> 'Check'
       OR L.requestStatus <> 'New Request'
       OR DATEDIFF(hour, CAST(L.requestDate AS DATE), GETDATE()) not between 0 and 48
       )