MySQL IF() to display custom YES or NO messages


Let us first create a table −

mysql> create table DemoTable1850
     (
     OrderStatus varchar(20)
     );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1850 values('Yes');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1850 values('No');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1850 values('Yes');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1850 values('Yes');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1850;

This will produce the following output −

+-------------+
| OrderStatus |
+-------------+
| Yes         |
| No          |
| Yes         |
| Yes         |
+-------------+
4 rows in set (0.00 sec)

Here is the query to implement IF() to display custom messages:

mysql> select if(OrderStatus='Yes','Order has been placed','Failed') as Status from DemoTable1850;

This will produce the following output −

+-----------------------+
| Status                |
+-----------------------+
| Order has been placed |
| Failed                |
| Order has been placed |
| Order has been placed |
+-----------------------+
4 rows in set (0.00 sec)

Updated on: 26-Dec-2019

445 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements