2015-09-05 90 views
0

我有一個關於SQL Server的問題。基於2列條件檢索記錄

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中完成此任務。

+0

只需使用括號以的條件進行評估,你打算在他們的方式。 –

回答

0

事情是這樣的:

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