

- 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
MySQL ORDER BY with different ordering for some of the values as descending and others ascending?
<p>You can use the field() for this. Let us first create a table −</p><pre class="result notranslate">mysql> create table DemoTable ( Value int ); Query OK, 0 rows affected (0.80 sec)</pre><p>Insert some records in the table using insert command −</p><pre class="prettyprint notranslate">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)</pre><p>Display all records from the table using select statement −</p><pre class="prettyprint notranslate">mysql> select *from DemoTable;</pre><h2>Output</h2><pre class="result notranslate">+-------+ | Value | +-------+ | 10 | | 70 | | 60 | | 56 | | 81 | | 85 | +-------+ 6 rows in set (0.00 sec)</pre><p>Following is the query to ORDER BY specific 3 values as descending and rest of the as ascending order −</p><pre class="prettyprint notranslate">mysql> select *from DemoTable order by field(Value,60,70,81) desc;</pre><h2>Output</h2><pre class="result notranslate">+-------+ | Value | +-------+ | 81 | | 70 | | 60 | | 10 | | 56 | | 85 | +-------+ 6 rows in set (0.00 sec)</pre>
- Related Questions & Answers
- 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 order by 0 first and then display the record in descending order?
- 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 command to order timestamp values in ascending order without using TIMESTAMP()?
- Order MySQL records randomly and display name in Ascending order
- 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?
- Using MySQL IN() for some column values with underscore
- How to order by timestamp (descending order) in MongoDB
- 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?
- MySQL Order by with case?
- MySQL query to order by NULL values
Advertisements