
- 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
Can we use IFNULL along with MySQL ORDER BY?
You can use IFNULL along with ORDER BY clause. The syntax is as follows −
SELECT *FROM yourTableName ORDER BY IFNULL(yourColumnName1,yourColumnName2);
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table IfNullDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> ProductName varchar(10), -> ProductWholePrice float, -> ProductRetailPrice float, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (1.19 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into IfNullDemo(ProductName,ProductWholePrice,ProductRetailPrice) values('Product-1',99.50,150.50); Query OK, 1 row affected (0.21 sec) mysql> insert into IfNullDemo(ProductName,ProductWholePrice,ProductRetailPrice) values('Product-2',NULL,76.56); Query OK, 1 row affected (0.18 sec) mysql> insert into IfNullDemo(ProductName,ProductWholePrice,ProductRetailPrice) values('Product-3',105.40,NULL); Query OK, 1 row affected (0.20 sec) mysql> insert into IfNullDemo(ProductName,ProductWholePrice,ProductRetailPrice) values('Product-4',NULL,NULL); Query OK, 1 row affected (0.18 sec) mysql> insert into IfNullDemo(ProductName,ProductWholePrice,ProductRetailPrice) values('Product-5',209.90,400.50); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from IfNullDemo;
The following is the output −
+----+-------------+-------------------+--------------------+ | Id | ProductName | ProductWholePrice | ProductRetailPrice | +----+-------------+-------------------+--------------------+ | 1 | Product-1 | 99.5 | 150.5 | | 2 | Product-2 | NULL | 76.56 | | 3 | Product-3 | 105.4 | NULL | | 4 | Product-4 | NULL | NULL | | 5 | Product-5 | 209.9 | 400.5 | +----+-------------+-------------------+--------------------+ 5 rows in set (0.02 sec)
Here is the query to order by if null −
mysql> select *from IfNullDemo order by ifnull(ProductWholePrice,ProductRetailPrice);
The following is the output −
+----+-------------+-------------------+--------------------+ | Id | ProductName | ProductWholePrice | ProductRetailPrice | +----+-------------+-------------------+--------------------+ | 4 | Product-4 | NULL | NULL | | 2 | Product-2 | NULL | 76.56 | | 1 | Product-1 | 99.5 | 150.5 | | 3 | Product-3 | 105.4 | NULL | | 5 | Product-5 | 209.9 | 400.5 | +----+-------------+-------------------+--------------------+ 5 rows in set (0.00 sec)
- Related Articles
- Can we use ORDER BY NULL in MySQL?
- How can we distinguish between MySQL IFNULL() and NULLIF() functions?
- How can we create the MySQL view with ORDER BY clause?
- How can we use MySQL REVERSE() function on column’s data along with WHERE clause?
- How can we use ORDER BY clause while calculating the Date?
- Why we cannot use MySQL DATE data type along with time value?
- How to use $ifNull with MongoDB aggregation?
- Can we order a MySQL result with mathematical operations?\n
- How can I use IFNULL() function at the place of COALESCE() function in MySQL?
- Can we use MySQL GROUP BY clause with multiple columns like MySQL DISTINCT clause is used?
- How can we use SIGNAL statement with MySQL triggers?
- Can we use backticks with column value in MySQL?
- What is the use of MySQL IFNULL() control flow function?
- How can we use ASCII() function with MySQL WHERE clause?
- How can we use CHAR_LENGTH() function with MySQL WHERE clause?

Advertisements