MySQL ORDER BY Query results
ORDER BY
The ORDER BY keyword in SQL syntax is used to sort query results.
Sorting is divided into ascending (ASC) and Descending (DESC), and the default is ascending when the sort method is not specified by using order.
Grammar:
SELECT column,... From Tb_name to Column1,column2,... DESC (ASC)
You must list the sorted field names after order by, which can be multiple fields.
To query the user table UID in descending order:
SELECT Uid,username from User order by UID DESC
The results of the query are as follows:
UID username
4 Xiao Wang
3 Jack
2 xiaoming
1 Admin
Example 2:
SELECT username from user order by RegDate DESC LIMIT 10
This example queries the user name of the newly registered 10-person user.
MySQL Limit Limited Query record number
MySQL LIMIT
The LIMIT keyword in MySQL is used to limit the maximum number of query records returned.
Grammar:
In the syntax, offset represents the offset (the cursor to the data record), and rows represents the maximum number of records that the query qualifies for return, which must be an integer.
Example:
SELECT username from user LIMIT 4,10
If the data in the user table has more than 14 records, the example returns a 第5-14条 record that matches the result (total 10), noting that the default offset is 0.
The offset parameter, if omitted, defaults to 0, that is, LIMIT 10 is equivalent to LIMIT 0,10 (returns the first 10 records that meet the query criteria).
Tips
Rows in LIMIT do not support the value-1 (all data from the current offset to the end of the table record), such as:
SELECT username from user LIMIT 9,-1
Running this SQL will result in parameter errors.
Small Tips
When you confirm that the query results have only one piece of data (such as checking user name password), you can add the limit 1 restrictions, when the system query to a data that stops searching without continuing to find the next record, which can effectively improve query efficiency.