Returning 1 or 0 in specific SQL query

对着背影说爱祢 提交于 2019-12-25 07:06:09

问题


I have three columns in the table MYTABLE (ID, NUM, NAMES). There is a column NAMES. I need to check on NAMES column to see if the first name is JACK or BRUCE and the corresponding NUM column = 0. If the match is found, return 1 else 0.

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

I've came up with the following query:

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

That does not work unfortunately.


回答1:


This works (SQLFiddle demo):

SELECT id, num,
    CASE WHEN names IN ('JACK', 'BRUCE') AND num=0
    THEN 1 ELSE 0 END AS mynames
FROM mytable



回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/14770092/returning-1-or-0-in-specific-sql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!