Sql 带有where子句的select语句
我有一个数据如下Sql 带有where子句的select语句,sql,sql-server,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为空或W_ID为5的记录 结果是 ID
ID Status W_ID
1 In Progress 7
2 In Progress 5
3 Complete 5
4 In Progress NULL
5 Complete 7
我希望选择状态为“进行中”且W_ID为空或W_ID为5的记录
结果是
ID Status W_ID
2 In Progress 5
4 In Progress NULL
我应该如何编写select查询?尝试如下,它非常简单,您不必应用“and”&“or”条件
Select * from table where Status = 'In Progress' and (W_ID is NULL or W_ID = 5)
或
尝试以下查询,它获取状态为“进行中”的记录
SELECT * FROM Table WHERE Status ='In Progress' AND (ISNULL(W_ID,0) = 0 OR ISNULL(W_ID,0) = 5)
您尝试过查询了吗?请尝试从Status='In Progress'和(W_ID为null或W_ID='5')中选择*
从Status='In Progress'和W_ID为null或W_ID=5的表中选择*,谢谢。为什么我如此愚蠢…只需一个简单的问题就可以解决。在where子句中保留“where Status!=‘Complete’”
WHERE Status = 'In Progress' AND ISNULL(W_ID,5)= 5
SELECT * FROM Table WHERE Status ='In Progress' AND (ISNULL(W_ID,0) = 0 OR ISNULL(W_ID,0) = 5)