
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Wrap around to first value and implement MySQL ORDER BY ASC and DESC in a single query
Let us first create a table −
mysql> create table DemoTable -> ( -> Value int -> ); Query OK, 0 rows affected (3.21 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.78 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.94 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.41 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(70); Query OK, 1 row affected (0.49 sec) mysql> insert into DemoTable values(80); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(9); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(8); Query OK, 1 row affected (0.38 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable ;
This will produce the following output −
+-------+ | Value | +-------+ | 20 | | 40 | | 30 | | 10 | | 90 | | 70 | | 80 | | 9 | | 8 | +-------+ 9 rows in set (0.00 sec)
Here is the query to wrap around” to the first value and implement ORDER BY ASC and DESC in a single query −
mysql> select *from DemoTable -> order by Value > 10 desc,Value asc;
This will produce the following output −
+-------+ | Value | +-------+ | 20 | | 30 | | 40 | | 70 | | 80 | | 90 | | 8 | | 9 | | 10 | +-------+ 9 rows in set (0.00 sec)
- Related Articles
- Implement MySQL ORDER BY without using ASC or DESC?
- Order by desc except a single value in MySQL
- How to ORDER BY DESC and display the first 3 records in MySQL?
- MySQL query to display ASC order in number column?
- How to sort varchar numeric columns by DESC or ASC in MySQL?
- Order By date ASC in MySQL?
- Fix a row value and then ORDER BY DESC rest of the values in MySQL
- MySQL query to first set negative value in descending order and then positive value in ascending order
- Implement SELECT LIKE and CHAR_LENGTH() in a single MySQL query
- A single MySQL query to select value from first table and insert in the second?
- MySQL ORDER BY ASC and display NULLs at the bottom?
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- Resolve Syntax error near “ORDER BY order DESC” in MySQL?
- Implement multiple COUNT() in a single MySQL query
- Implement MySQL LIMIT and OFFSET in a single query stating its difference

Advertisements