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.

grammar:

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
4Marry
3Jack
2John
1Admin

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 DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook