
- 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
MySQL Order by a specific column x and display remaining values in ascending order
Let us first create a table −
mysql> create table DemoTable -> ( -> MonthNumber int -> ); Query OK, 0 rows affected (1.68 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(9); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(6); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(8); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(5); 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 −
+-------------+ | MonthNumber | +-------------+ | 10 | | 1 | | 9 | | 6 | | 8 | | 5 | +-------------+ 6 rows in set (0.00 sec)
Following is the query to order by a specific column and display remaining records in ascending order −
mysql> select * from DemoTable -> order by MonthNumber=5 desc,MonthNumber asc;
This will produce the following output −
+-------------+ | MonthNumber | +-------------+ | 5 | | 1 | | 6 | | 8 | | 9 | | 10 | +-------------+ 6 rows in set (0.00 sec)
- Related Articles
- Order MySQL records randomly and display name in Ascending order
- MySQL command to order timestamp values in ascending order?
- MySQL command to order timestamp values in ascending order without using TIMESTAMP()?
- ORDER BY a specific word in MySQL
- MySQL Order By specific strings?
- Query non-empty values of a row first in ascending order and then display NULL values
- Order by on a specific number in MySQL?
- MySQL order by 0 first and then display the record in descending order?
- How to Order by a specific string in MySQL?
- MySQL ORDER BY with different ordering for some of the values as descending and others ascending?
- MySQL query to display ASC order in number column?
- MySQL query to order by two fields and NULL values in chronological order?
- Order By Length of Column in MySQL
- ORDER BY specific field value first in MySQL
- Order by a single field and display rest of the records in the same order with MySQL

Advertisements