select语句with where子句

时间:2017-11-20 09:34:05

标签: sql sql-server

我有以下数据

ID           Status          W_ID
1          In Progress        7
2          In Progress        5
3          Complete           5
4          In Progress       NULL
5          Complete           7

我希望选择记录状态为“进行中”,W_ID为NULL或W_ID为5

结果就像

ID           Status          W_ID
2          In Progress        5
4          In Progress       NULL

我应该如何编写选择查询?

3 个答案:

答案 0 :(得分:0)

尝试如下,非常简单,您必须应用'和' &安培; '或'条件。

Select * from table where Status = 'In Progress' and  (W_ID is NULL or W_ID = 5)

答案 1 :(得分:0)

WHERE Status = 'In Progress' AND (W_ID IS NULL OR W_ID = 5)  

或者

WHERE Status = 'In Progress' AND ISNULL(W_ID,5)= 5

答案 2 :(得分:0)

尝试以下查询,它以状态

的状态获取正在进行的记录
SELECT * FROM Table WHERE Status ='In Progress' AND (ISNULL(W_ID,0) = 0 OR ISNULL(W_ID,0) = 5)