Fix a row value and then ORDER BY DESC rest of the values in MySQL



Let us first create a table −

mysql> create table DemoTable (    id int ); Query OK, 0 rows affected (0.52 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable values(7); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(8); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(6); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(9); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(11); Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------+ | id   | +------+ | 10 | | 7 | | 8 | | 6 | | 9 | | 11 | +------+ 6 rows in set (0.00 sec)

Following is the query to fix a row value and then ORDER BY DESC rest of the values −

mysql> select *from DemoTable order by id=10 DESC,id asc;

This will produce the following output −

+------+ | id | +------+ | 10 | | 6 | | 7 | | 8 | | 9 | | 11 | +------+ 6 rows in set (0.00 sec)
Updated on: 2019-09-26T08:08:56+05:30

122 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements