- 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
MySQL ORDER BY with different ordering for some of the values as descending and others ascending?
You can use the field() for this. Let us first create a table −
mysql> create table DemoTable ( Value int ); Query OK, 0 rows affected (0.80 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(70); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(60); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(56); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(81); Query OK, 1 row affected (0.44 sec) mysql> insert into DemoTable values(85); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+-------+ | Value | +-------+ | 10 | | 70 | | 60 | | 56 | | 81 | | 85 | +-------+ 6 rows in set (0.00 sec)
Following is the query to ORDER BY specific 3 values as descending and rest of the as ascending order −
mysql> select *from DemoTable order by field(Value,60,70,81) desc;
Output
+-------+ | Value | +-------+ | 81 | | 70 | | 60 | | 10 | | 56 | | 85 | +-------+ 6 rows in set (0.00 sec)
- Related Articles
- How to arrange the fractions in ascending order and descending order?
- MySQL Order by a specific column x and display remaining values in ascending order
- MySQL query to ORDER BY `user_id` IN (1,2,3) AND `name` for custom ordering
- MySQL command to order timestamp values in ascending order?
- MySQL query to first set negative value in descending order and then positive value in ascending order
- MySQL order by 0 first and then display the record in descending order?
- MySQL command to order timestamp values in ascending order without using TIMESTAMP()?
- Is it possible to divide records in both ascending and descending order in MySQL and display them alternatively?
- Sort by date & time in descending order in MySQL?
- Order MySQL records randomly and display name in Ascending order
- Using MySQL IN() for some column values with underscore
- How to sort one column of an R data frame in ascending and the other in descending order?
- MySQL Order By date column and integer column, but specify ordering rules of integer column? Is it possible?
- Is it possible to sort varchar data in ascending order that have both string and number values with MySQL?
- MySQL query to get the count of values and display the count in a new column ordered in descending order

Advertisements