Example of limit usage and simple sequential lookup in MySQL

Source: Internet
Author: User

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:

orderby id desclimit 10 按照id的倒序排序 取出前10条

orderby id desclimit 0,10 按照id的倒序排序 取出前10条

In other words, limit n is equivalent to limit 0,n.

如:正序排列取中间几行数据,默认从0开始

orderbyid 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.