Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use limit in mysql

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to use limit in mysql. The code is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Limit syntax supports two parameters, offset and limit, the former indicating offset and the latter indicating fetching the previous limit data.

The limit in mysql can be used to force the SELECT statement to return a specified number of records. LIMIT accepts one or two numeric parameters, which must be an integer constant. If two parameters are given, the first parameter specifies the offset of the first returned record row, and the second parameter specifies the maximum number of record rows returned.

When we use query statements, we often have to return the first few rows or rows of data in the middle. What should we do at this time? Don't worry, mysql has already provided us with such a feature

SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset

The LIMIT clause can be used to force the SELECT statement to return a specified number of records. LIMIT accepts one or two numeric parameters. Parameter must be an integer constant. Given two parameters, the first parameter specifies the offset of the first returned record row, and the second parameter specifies the maximum number of record rows returned. The offset of the initial record line is 0 (not 1): for compatibility with PostgreSQL, MySQL also supports syntax: LIMIT # OFFSET #.

Mysql > SELECT * FROM table LIMIT 5 mysql 10; / / retrieve record rows 6-15 / / to retrieve all record rows from a certain offset to the end of the recordset, you can specify the second parameter-1: mysql > SELECT * FROM table LIMIT 95 line 96-last. / / if only one parameter is given, it represents the maximum number of rows returned: mysql > SELECT * FROM table LIMIT 5; / / retrieve the first five rows of records / / in other words, LIMIT n is equivalent to LIMIT 0line n. Mysql > SELECT * FROM table LIMIT 5 Lines 10; / retrieve record rows 6-15 FROM table LIMIT / in order to retrieve all record rows from a certain offset to the end of the recordset, you can specify the second parameter-1: mysql > SELECT * FROM table LIMIT 95 FROM table LIMIT 1; / / retrieve record rows if given only one parameter, it represents the maximum number of record rows returned: mysql > SELECT * record 5 / / retrieve the first five rows of records / / in other words, LIMIT n is equivalent to LIMIT 0penny n.

It is often said that the execution efficiency of Limit is high, for a specific condition, that is, the number of databases is very large, but only part of the data needs to be queried.

The principle of high efficiency is to avoid full table scanning and improve query efficiency.

For example: each user's email is unique, if the user logs in with email as the user name, they need to query a record corresponding to email.

SELECT * FROM t_user WHERE email=?

The above statement implements the query of a piece of user information corresponding to email, but because the column email is not indexed, it will lead to a full table scan and the efficiency will be very low.

SELECT * FROM t_user WHERE email=? LIMIT 1

With LIMIT 1, as long as a corresponding record is found, it will not continue to scan down, and the efficiency will be greatly improved.

The above is how to use limit in mysql. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report