Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning 1 or 0 in specific SQL query

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.

like image 340
minerals Avatar asked Jan 14 '23 06:01

minerals


1 Answers

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
like image 166
mvp Avatar answered Jan 17 '23 04:01

mvp