- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements