Use IF statement in MySQL
Question:
How to use IF statement in MySQL? Answer:
SELECT IF(status = 1, 'ACTIVE', 'INACTIVE') FROM task;
Description:
In MySQL, if you want a simple filed value conversion depending on a condition you can use the IF
statement. For example, if you have a field that stores whether the actual task is active or not and is stored as a TINYINT
. In this case, 0
means Inactive and 1
means Active. To return a human-readable format in an SQL query use the IF
function as follows:
SELECT IF(status = 1, 'Active', 'Inactive') from task;
Reference:
MySQL IF reference
Share "How to use IF statement in MySQL?"
Related snippets:
Tags:
if, statement, use, then, MySQL, MariaDB, sql, query Technical term:
Use IF statement in MySQL