Case clause syntax in MySQL or MariaDB

Example of CASE syntax in MySQL or MariaDB

CASE Statement is like the match condition and returns the value in the Select statement. Once the value matches it will return the value otherwise ELSE value return.

SYNTAX:

CASE match_value
    WHEN match_value1 THEN statement1
    WHEN match_value2 THEN statement2
    ELSE statement_list
END CASE

OR

CASE
    WHEN condition1 THEN statement1
    WHEN condition2 THEN statement2
    ELSE statement3 
END CASE

Example of using CASE statement in MySQL Database:

MariaDB [classicmodels]> 
select 
case when IFNULL(state,'')='' then country 
else state 
end as 'State',
country from offices;

+------------+-----------+
| State      | country   |
+------------+-----------+
| CA         | USA       |
| MA         | USA       |
| NY         | USA       |
| France     | France    |
| Chiyoda-Ku | Japan     |
| Australia  | Australia |
| UK         | UK        |
+------------+-----------+

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.