
- 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 implement MySQL ORDER BY x where (x=col3 if col3!=null, else x=col2)?
For this, you can use ORDER BY IFNULL(). Let us first create a table −
mysql> create table DemoTable -> ( -> Name varchar(20), -> CountryName varchar(20) -> ); Query OK, 0 rows affected (0.61 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris',NULL); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('David','AUS'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(NULL,'UK'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(NULL,'AUS'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(NULL,NULL); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+-------------+ | Name | CountryName | +-------+-------------+ | Chris | NULL | | David | AUS | | NULL | UK | | NULL | AUS | | NULL | NULL | +-------+-------------+ 5 rows in set (0.00 sec)
Here is the query to implement MySQL ORDER BY x where (x=col3 if col3!=null, else x=col2) −
mysql> select *from DemoTable -> order by ifnull(Name,CountryName);
This will produce the following output −
+-------+-------------+ | Name | CountryName | +-------+-------------+ | NULL | NULL | | NULL | AUS | | Chris | NULL | | David | AUS | | NULL | UK | +-------+-------------+ 5 rows in set (0.00 sec)
- Related Articles
- MySQL IF/WHEN/ELSE/OR with ORDER BY FIELD
- MySQL Order by a specific column x and display remaining values in ascending order
- If the polynomial $x^{19}+x^{17}+x^{13}+x^{11}+x^{7}+x^{5}+x^{3}$ is divided by $( x^{2}-1)$, then find the remainder.
- Implement If else in stored procedure in MySQL?
- MySQL query to order by NULL values
- MySQL where column = 'x, y, z'?
- Sum if all rows are not null else return null in MySQL?
- Insert five rational numbers between $x$ and $|x|$, where $x=\frac{-17}{20}$.
- Update a column A if null, else update column B, else if both columns are not null do nothing with MySQL
- If \( x+\frac{10 x}{100}=100 \)Find \( x \).
- If \( 27^{x}=\frac{9}{3^{x}} \), find \( x \).
- Find $x$ if:$\frac{5}{6}(x\ -\ 1)\ =\ x\ -\ 5$
- Determine $(8x)^x$, if $9^{x+ 2} = 240 + 9^x$.
- If \( x^{2}+\frac{1}{x^{2}}=62, \) find the value of\( x+\frac{1}{x} \)\( x-\frac{1}{x} \)
- If \( x+\frac{1}{x}=3 \), calculate \( x^{2}+\frac{1}{x^{2}}, x^{3}+\frac{1}{x^{3}} \) and \( x^{4}+\frac{1}{x^{4}} \).

Advertisements