Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to do conditional logic in a SQL query?

So I have a fairly long complex query but the gist is that the query's basic functionality looks like this SELECT verification_id FROM verification_table

The verification_id returns an integer from 0-3. Is there a way to do something in the SQL query where if the verification_id is 0, it returns a string like "new" and different ones for all 4 verification_id's.

I can do this in the backend via PHP but i was wondering if there was a way to do it via MySQL

like image 619
raeq Avatar asked Dec 04 '25 07:12

raeq


1 Answers

You want a case statement. It can be used for boolean conditionals as well as for selecting between multiple outcomes...

SELECT [Condition]
    CASE WHEN TRUE THEN 'True'
    ELSE 'False'
END 
FROM [Table]

Note that the return type of each branch must be the same - The column can only be of one type

like image 143
Basic Avatar answered Dec 07 '25 00:12

Basic