- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
What is the proper way to insert an IF statement into a MySQL query?
To insert an IF statement into a MySQL query, use the below syntax::
select yourColumnName ,if(yourCondition, yourStatement1,yourStatement2) from yourTableName;
Let us first create a table −
mysql> create table DemoTable1571 -> ( -> Id int, -> Value int -> ); Query OK, 0 rows affected (5.63 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1571 values(101,500); Query OK, 1 row affected (1.07 sec) mysql> insert into DemoTable1571 values(102,450); Query OK, 1 row affected (0.47 sec) mysql> insert into DemoTable1571 values(103,300); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1571 values(104,700); Query OK, 1 row affected (0.23 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1571;
This will produce the following output
+------+-------+ | Id | Value | +------+-------+ | 101 | 500 | | 102 | 450 | | 103 | 300 | | 104 | 700 | +------+-------+ 4 rows in set (0.00 sec)
Here is the query to properly insert an if statement into a MySQL query.
mysql> select Value,if(Value > 550, 'Value is greater than 550','Value is less than 550') from DemoTable1571;
This will produce the following output −
+-------+-----------------------------------------------------------------------+ | Value | if(Value > 550, 'Value is greater than 550','Value is less than 550') | +-------+-----------------------------------------------------------------------+ | 500 | Value is less than 550 | | 450 | Value is less than 550 | | 300 | Value is less than 550 | | 700 | Value is greater than 550 | +-------+-----------------------------------------------------------------------+ 4 rows in set (0.00 sec)
- Related Articles
- Is there a way to name columns in an INSERT statement in MySQL?
- What is the fastest way to insert a large number of rows into a MySQL table?
- Fastest way to insert with multiple values in a single MySQL query?
- How to insert into two tables using a single MySQL query?
- What is the use of MySQL IGNORE INSERT statement?
- What is the proper way to retrieve the value stored in INT column as MySQL TIMESTAMP?
- What MySQL returns if I insert invalid value into ENUM?
- Is it possible to make an insert or an update in the same MySQL query?
- MySQL query for INSERT INTO using values from another table?
- How to add static value while INSERT INTO with SELECT in a MySQL query?
- MySQL query to return a string as a result of IF statement?
- MySQL statement to copy data from one table and insert into another table
- The easiest way to insert date records in MySQL?
- What MySQL returns on running the INSERT INTO statement without giving the column name and values both?
- Is there a need to insert auto_increment column values in MySQL while using INSERT statement?

Advertisements