

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Order MySQL records randomly and display name in Ascending order
- MySQL command to order timestamp values in ascending order?
- MySQL Order By specific strings?
- ORDER BY a specific word in MySQL
- MySQL command to order timestamp values in ascending order without using TIMESTAMP()?
- Order by on a specific number in MySQL?
- Query non-empty values of a row first in ascending order and then display NULL values
- MySQL order by 0 first and then display the record in descending order?
- How to Order by a specific string in MySQL?
- 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
- MySQL ORDER BY with different ordering for some of the values as descending and others ascending?
- MySQL query to order by NULL values
- MySQL ORDER BY ASC and display NULLs at the bottom?
Advertisements