- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements