- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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)
- Related Articles
- Order by desc except a single value in MySQL
- Fix a specific column value and display random values for rest of the rows in MySQL
- MySQL query to order records but fix a specific name and display rest of the values (only some) random
- Resolve Syntax error near “ORDER BY order DESC” in MySQL?
- Wrap around to first value and implement MySQL ORDER BY ASC and DESC in a single query
- How to ORDER BY DESC and display the first 3 records in MySQL?
- How to order DESC by a field, but list the NULL values first?
- Implement MySQL ORDER BY without using ASC or DESC?
- Query non-empty values of a row first in ascending order and then display NULL values
- Order by a single field and display rest of the records in the same order with MySQL
- Select items based on value first, then order on the basis of date for rest of the records in MySQL
- MySQL order by 0 first and then display the record in descending order?
- Ignore null values in MySQL and display rest of the values
- Decrease a row value by 1 in MySQL?
- MySQL query to first set negative value in descending order and then positive value in ascending order

Advertisements