
- 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
How to use if/else condition in select in MySQL?
Let us first create a table −
mysql> create table DemoTable1966 ( UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserName varchar(20), PhotoLiked int ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1966(UserName,PhotoLiked) values('Chris',57); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1966(UserName,PhotoLiked) values('David',100); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1966(UserName,PhotoLiked) values('Mike',68); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1966(UserName,PhotoLiked) values('Sam',78); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1966;
This will produce the following output −
+--------+----------+------------+ | UserId | UserName | PhotoLiked | +--------+----------+------------+ | 1 | Chris | 57 | | 2 | David | 100 | | 3 | Mike | 68 | | 4 | Sam | 78 | +--------+----------+------------+ 4 rows in set (0.00 sec)
Here is the query to use if/else condition in SELECT with MySQL −
mysql> select if(PhotoLiked > 75,UserName,'Likes are less') as Result from DemoTable1966;
This will produce the following output −
+------------------------------------------------+ | Result | +------------------------------------------------+ | Likes are less | | David | | Likes are less | | Sam | +------------------------------------------------+ 4 rows in set (0.00 sec)
- Related Articles
- What is the ‘if...else if...else’ statement in Java and how to use it?
- How to use IF in stored procedure and select in MySQL?
- How to select rows with condition via concatenate in MySQL?
- How to use #if..#elif…#else…#endif directives in C#?
- Is there such thing as a SELECT with an IF/ELSE statement in MySQL?
- How to use “OR” condition in MySQL CASE expression?\n
- How to use NULL in MySQL SELECT statement?
- How to use alias in MySQL select query?
- How to use OR condition in a JavaScript IF statement?
- How to use if...else statement at the command line in Python?
- How to use MySQL JOIN without ON condition?
- IF ELSE statement in a MySQL Statement?
- How to use count with CASE condition in a MySQL query?
- Implement If else in stored procedure in MySQL?
- What is the ‘if else’ statement in Java and how to use it?

Advertisements