Get another order after limit with MySQL?


You need to use subquery with select statement, one select for inner and one for outer. The inner select will return rows and outer will order by ascending order. The syntax is as follows:

SELECT *FROM
(
   SELECT *FROM yourTableName ORDER BY yourColumnName1 DESC LIMIT 9
) AS anyAliasName
ORDER BY yourColumnName2;

To understand the above syntax, let us create a table. The query to create a table is as follows:

mysql> create table OrderByAfterLimit
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> UserName varchar(20),
   -> UserAge int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.69 sec)

Now you can insert some records in the table using insert command. The query is as follows:

mysql> insert into OrderByAfterLimit(UserName, UserAge) values('John',23);
Query OK, 1 row affected (0.24 sec)
mysql> insert into OrderByAfterLimit(UserName, UserAge) values('Larry',21);
Query OK, 1 row affected (0.36 sec)
mysql> insert into OrderByAfterLimit(UserName, UserAge) values('Bob',26);
Query OK, 1 row affected (0.19 sec)
mysql> insert into OrderByAfterLimit(UserName, UserAge) values('James',22);
Query OK, 1 row affected (0.18 sec)
mysql> insert into OrderByAfterLimit(UserName, UserAge) values('Robert',28);
Query OK, 1 row affected (0.20 sec)
mysql> insert into OrderByAfterLimit(UserName, UserAge) values('Mike',29);
Query OK, 1 row affected (0.19 sec)
mysql> insert into OrderByAfterLimit(UserName, UserAge) values('Carol',24);
Query OK, 1 row affected (0.13 sec)
mysql> insert into OrderByAfterLimit(UserName, UserAge) values('David',20);
Query OK, 1 row affected (0.23 sec)
mysql> insert into OrderByAfterLimit(UserName, UserAge) values('Maxwell',30);
Query OK, 1 row affected (0.46 sec)

Display all records from the table using select statement. The query is as follows:

mysql> select *from OrderByAfterLimit;

The following is the output:

+----+----------+---------+
| Id | UserName | UserAge |
+----+----------+---------+
|  1 | John     |      23 |
|  2 | Larry    |      21 |
|  3 | Bob      |      26 |
|  4 | James    |      22 |
|  5 | Robert   |      28 |
|  6 | Mike     |      29 |
|  7 | Carol    |      24 |
|  8 | David    |      20 |
|  9 | Maxwell  |      30 |
+----+----------+---------+
9 rows in set (0.00 sec)

Here is the query to order by after limit:

mysql> select *from
   -> (
   -> select *from OrderByAfterLimit order by UserAge DESC LIMIT 9) AS tbl1
   -> order by UserName;

The following is the output:

+----+----------+---------+
| Id | UserName | UserAge |
+----+----------+---------+
|  3 | Bob      |      26 |
|  7 | Carol    |      24 |
|  8 | David    |      20 |
|  4 | James    |      22 |
|  1 | John     |      23 |
|  2 | Larry    |      21 |
|  9 | Maxwell  |      30 |
|  6 | Mike     |      29 |
|  5 | Robert   |      28 |
+----+----------+---------+
9 rows in set (0.00 sec)

Updated on: 30-Jul-2019

168 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements