2013-02-08 78 views
0

我在表MYTABLE(ID,NUM,NAMES)中有三列。有一列NAMES。我需要檢查名稱列,看的第一個名字是傑克或布魯斯和相應的NUM列= 0。如果發現匹配,返回1,否則爲0。在特定的SQL查詢中返回1或0

ID NUM NAMES  
1 1 'TOM' 
2 1 'MIKE' 
3 0 'JACK' 
4 1 'MICKY' 
5 0 'BRUCE' 

我已經想出了以下查詢:

select * 
    case NAMES in ('JACK', 'BRUCE') and NUM=0 then 1 else 0 end as MYNAMES 
from MYTABLE; 

不幸的是,這並不奏效。

回答

1

此作品(SQLFiddle demo):

SELECT id, num, 
    CASE WHEN names IN ('JACK', 'BRUCE') AND num=0 
    THEN 1 ELSE 0 END AS mynames 
FROM mytable 
3
select case 
     when exists 
     (
     select * 
     from YourTable 
     where name in ('JACK', 'BRUCE') 
       and NUM = 0 
     ) 
     then 1 
     else 0 
     end 
from dual 

Live example at SQL Fiddle.

+0

謝謝,有點不同的比我想象 – minerals

0
select case when NAMES in ('JACK','BRUCE') AND NUM = 0 
      then 1 
      else 0 
     end 
from your_table