
- 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 can I use a SELECT statement as an argument of MySQL IF() function?
It is quite possible to use a SELECT statement as the first argument of MySQL IF() function. To make it understand, consider the data as follows from table ‘Students’.
mysql> Select * from Students; +----+-----------+-----------+----------+----------------+ | id | Name | Country | Language | Course | +----+-----------+-----------+----------+----------------+ | 1 | Francis | UK | English | Literature | | 2 | Rick | USA | English | History | | 3 | Correy | USA | English | Computers | | 4 | Shane | France | French | Computers | | 5 | Validimir | Russia | Russian | Computers | | 6 | Steve | Australia | English | Geoinformatics | | 7 | Rahul | India | Hindi | Yoga | | 8 | Harshit | India | Hindi | Computers | | 9 | Harry | NZ | English | Electronics | +----+-----------+-----------+----------+----------------+ 9 rows in set (0.00 sec)
Now the following query will use SELECT Statement within IF() function
mysql> Select ID, IF((Select COUNT(*) FROM Students WHERE Language = 'English')>(Select COUNT(*) from Students WHERE Language <> 'English'),(CONCAT("Name is ", Name)),(CONCAT("Course is ", Course))) AS 'Name/Course' from Students; +----+-------------------+ | ID | Name/Course | +----+-------------------+ | 1 | Name is Francis | | 2 | Name is Rick | | 3 | Name is Correy | | 4 | Name is Shane | | 5 | Name is Validimir | | 6 | Name is Steve | | 7 | Name is Rahul | | 8 | Name is Harshit | | 9 | Name is Harry | +----+-------------------+ 9 rows in set (0.00 sec)
- Related Articles
- How can I use MySQL IF() function within SELECT statement?
- IF() function in a MySQL Select statement?
- What MySQL returns if I provide a non-hexadecimal number as an argument to UNHEX() function?
- How to use the CAST function in a MySQL SELECT statement?
- Can we use SELECT NULL statement in a MySQL query?
- What happens if we provide NULL as an argument to MySQL CHAR() function?
- Is there such thing as a SELECT with an IF/ELSE statement in MySQL?
- How can we use MySQL SELECT statement to count number of rows in a table?
- What MySQL returns if we use NULL, as both the arguments, as one of the argument and as a separator, in CONCAT_WS() function?
- How to use NULL in MySQL SELECT statement?
- What MySQL returns if we use UNIX_TIMESTAMP() function with no argument?
- How MySQL evaluates if I will use an expression within SUM() function?
- How to use a select statement while updating in MySQL?
- How can we use a JavaScript function as an object?
- How can I use MySQL INTERVAL() function with a column of a table?

Advertisements