Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Case with multiple values

Tags:

sql

Is there a condensed form of the following statement?

SELECT Name, Case StatusID WHEN 1 THEN 'Alive' WHEN 2 THEN 'Alive' WHEN 3 THEN 'Alive' WHEN 4 THEN 'Dying' ELSE 'Dead' END FROM People

for example

CASE StatusID WHEN 1,2,3 THEN 'Alive'

or

CASE StatusID WHEN 1 OR 2 OR 3 THEN 'Alive'
like image 843
Marcel Avatar asked Dec 13 '22 05:12

Marcel


1 Answers

depending on the DB you use the following will do the trick

SELECT 
Name, 
Case WHEN StatusID  IN ( 1, 2, 3 ) THEN 'Alive' WHEN StatusID = 4 THEN 'Dying' ELSE 'Dead' END 
FROM People
like image 125
Yahia Avatar answered Dec 14 '22 19:12

Yahia