根据2列条件检索记录

时间:2015-09-05 16:20:38

标签: sql sql-server-2008 sql-server-2012

我对SQL Server有疑问。

Table : location

  Id  | status  | locid
  ----+---------+-------
  1   | D       |10
  2   | D       |
  3   | C       |20
  4   | A       |
  5   | D       |
  6   | F       |
  7   |         |20
  8   |         |    

此处,根据条件locid is empty or null and status !='d',我们需要检索该记录。

基于上表我想要输出如下:

Id  |  status   | locid
----+-----------+-------

 4   |  A        |
 6   |  F        |
 8   |           |

我尝试了这个查询:

select * 
from location 
where  status!= 'D' and locid='' or locid is null

但它没有返回预期的结果。请告诉我如何编写查询以在SQL Server中执行此任务。

1 个答案:

答案 0 :(得分:0)

这样的事情:

select * 
from location
where  status <> 'D' and isnull(locid,'')=''
相关问题