Limit receives one or two parameters.
You can implement data in the first few rows of the row or rows of data, or data in the middle of a row
order
by + 关键字
indicates the order BY keyword, the default is the positive sequence, from small to large,
The ORDER by + keyword desc is in reverse order, from large to small.
such as: In reverse order to take the first few rows of data:
order
by
id
desc
limit 10 按照id的倒序排序 取出前10条
order
by
id
desc
limit 0,10 按照id的倒序排序 取出前10条
In other words, limit n is equivalent to limit 0,n.
如:正序排列取中间几行数据,默认从0开始
order
by
id limit 5,10 表示按照id的正序排序 从第6条开始取10条,
即取出第6行到第15行数据
such as: Remove data from a row to the last row:
You can specify that the second parameter is-1, which represents the last row
SELECT * from table LIMIT 95,-1; Retrieves all data from 96 rows to the last row in the end of the document.
Example
(reproduced):
Suppose there is a database table User_info, the first column ID, used as a unique identifier, the second column is user_name, the user name, the third is Some_data, a value is stored,
Find out in this table, in descending order of the third column values, all the user records in the top 5
Solution:
Select * from Order by desc 5;
But there may be duplicate data, use the DISTINCT keyword
Select * from where >= (Selectdistinctfromorderbydesc4, 1 )orderbydesc;
Example of limit usage and simple sequential lookup in MySQL