MySQL Select order by

The ORDER BY keyword in SQL syntax is used to sort query results.

Sorting is divided into ascending (ASC) and descending (DESC), when you do not use ORDER BY to specify the sorting, the default is ascending.


SELECT column, ... FROM tb_name ORDER BY column1, column2, ... DESC (ASC)

ORDER BY must be listed behind the sorted field name, can be multiple fields.

Query the user table uid in descending order:

SELECT uid, username FROM user ORDER BY uid DESC

The results are as follows:

uid username

Example 2:

SELECT username FROM user ORDER BY regdate DESC LIMIT 10

This example queries the user name of the newly registered 10 users.

Copyright 2014-2017 by All Rights Reserved.