

- 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
Order by selected record in MySQL?
You can use a CASE statement for this. Let us first create a table −
mysql> create table DemoTable ( Number int ); Query OK, 0 rows affected (0.71 sec)
Insert records in the table using insert command −
mysql> insert into DemoTable values(490); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable values(310); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(540); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(123); Query OK, 1 row affected (0.60 sec) mysql> insert into DemoTable values(1230); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(1090); Query OK, 1 row affected (0.43 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select * from DemoTable;
This will produce the following output −
+--------+ | Number | +--------+ | 490 | | 310 | | 540 | | 123 | | 1230 | | 1090 | +--------+ 6 rows in set (0.00 sec)
Following is the query to order by selected record in MySQL −
mysql> select *from DemoTable order by (case when Number = 1090 then 0 else 1 end), Number;
This will produce the following output −
+--------+ | Number | +--------+ | 1090 | | 123 | | 310 | | 490 | | 540 | | 1230 | +--------+ 6 rows in set (0.00 sec)
- Related Questions & Answers
- MySQL order by 0 first and then display the record in descending order?
- Get the returned record set ordered by (ORDER BY) position in MySQL 'IN' clause
- Get the returned record set order in MySQL IN clause?
- MySQL query to fetch record by year
- MySQL Order By specific strings?
- MySQL Order by with case?
- MySQL Order by beginning letter?
- Order By date ASC in MySQL?
- Order date records and fetch the 2nd ordered record in MySQL
- MySQL order by string with numbers?
- Perform MySQL ORDER BY keyword match?
- MySQL ORDER BY with EXPLAIN command
- MySQL ORDER BY with CASE WHEN
- MySQL ORDER BY strings with underscore?
- Order MySQL query by multiple ids?
Advertisements