
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
IF() function in a MySQL Select statement?
The IF() function returns a value based on a condition.
The syntax is as follows−
SELECT IF(yourCondition, yourMessageIfConditionBecomesTrue,yourMessageIfConditionBecomesFalse) from yourTableName; Let us first create a table: mysql> create table DemoTable ( Value int ); Query OK, 0 rows affected (0.60 sec)
Insert records in the table using insert command −
mysql> insert into DemoTable values(1000); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(2000); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(500); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(1100); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Value | +-------+ | 1000 | | 2000 | | 500 | | 1100 | +-------+ 4 rows in set (0.00 sec)
Here is the query to implement IF() in MySQL −
mysql> select Value,IF(Value > 1000, CONCAT(Value,' is greater than 1000'),CONCAT(Value,' is lower than 1000')) AS Result from DemoTable;
This will produce the following output−
+-------+---------------------------+ | Value | Result | +-------+---------------------------+ | 1000 | 1000 is lower than 1000 | | 2000 | 2000 is greater than 1000 | | 500 | 500 is lower than 1000 | | 1100 | 1100 is greater than 1000 | +-------+---------------------------+ 4 rows in set (0.00 sec)
- Related Articles
- How can I use MySQL IF() function within SELECT statement?
- MySQL SELECT IF statement with OR?
- How can I use a SELECT statement as an argument of MySQL IF() function?
- How to use the CAST function in a MySQL SELECT statement?
- MySQL case statement inside a select statement?
- IF ELSE statement in a MySQL Statement?
- How MySQL SUM() function evaluates if it is used with SELECT statement that returns no matching rows?
- Is there such thing as a SELECT with an IF/ELSE statement in MySQL?
- MySQL procedure to display a “select” statement twice
- Can we use SELECT NULL statement in a MySQL query?
- How to use a select statement while updating in MySQL?
- How to use NULL in MySQL SELECT statement?
- Get table names using SELECT statement in MySQL?
- Can you allow a regex match in a MySQL Select statement?
- Storing value from a MySQL SELECT statement to a variable?

Advertisements