如果我在mysql查询上使用LIMIT,结果集是否应该等于限制?

时间:2022-09-18 13:22:15

for example will select * from table limit 0,5
return at most 5 rows or
must it find exactly 5 and if the row_count doesnt equal 5, it returns an empty result set?

例如,将从表限制0,5中选择*返回最多5行,或者必须准确找到5,如果row_count不等于5,则返回空结果集?


what if the query was select * from table limit 5?

如果查询是从表限制5中选择*怎么办?

4 个解决方案

#1


Query SELECT * FROM table LIMIT 0,5 will return 5 records starting from the first record.

查询SELECT * FROM表LIMIT 0,5将从第一条记录开始返回5条记录。

Query SELECT * FROM table LIMIT 5 will also give the same result as above query.

查询SELECT * FROM表LIMIT 5也将提供与上述查询相同的结果。

If in that table there are fewer than 5 records then it will not fail but return whatever records are there.

如果在该表中少于5个记录,则它不会失败但返回那里的任何记录。

Query SELECT * FROM table LIMIT 6,5 will return record 7,8,9,10,11 as the index starts from 0.

查询SELECT * FROM表LIMIT 6,5将返回记录7,8,9,10,11,因为索引从0开始。

#2


http://dev.mysql.com/doc/refman/5.1/en/select.html

"The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be non-negative integer constants (except when using prepared statements). With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return."

“LIMIT子句可以用来约束SELECT语句返回的行数.LIMIT需要一个或两个数字参数,它们都必须是非负整数常量(使用预处理语句时除外)。有两个参数,第一个argument指定要返回的第一行的偏移量,第二行指定要返回的最大行数。“

So, to answer your question directly, it would return at most 5 rows.

因此,要直接回答您的问题,它最多会返回5行。

#3


The limit is, well, a limit, so it won't return more than that many rows. It can return less.

限制是一个限制,因此它不会返回超过那么多行。它可以减少回报。

#4


In query "select * from table limit 0,5" 0 does not specify the minimum records to return. It specifies the OFFSET. So when you say 0, if the query "select * from table" returns 10 records, "limit 0,5" will return the first 5. If you use "limit 5,5" it will return last 5 records.

在查询“select * from table limit 0,5”0中没有指定要返回的最小记录。它指定OFFSET。所以当你说0时,如果查询“select * from table”返回10条记录,“limit 0,5”将返回第5条。如果使用“limit 5,5”,它将返回最后5条记录。

If you have only 2 records, it will return two records. It will not return an error if you have no results. LIMIT is the maximum limit. Minimum can be anything, even 0 records.

如果您只有2条记录,它将返回两条记录。如果没有结果,它将不会返回错误。 LIMIT是最大限制。最小值可以是任何值,甚至是0记录。

"select * from table limit 5" is the same as "select * from table limit 0,5"

“select * from table limit 5”与“select * from table limit 0,5”相同

#1


Query SELECT * FROM table LIMIT 0,5 will return 5 records starting from the first record.

查询SELECT * FROM表LIMIT 0,5将从第一条记录开始返回5条记录。

Query SELECT * FROM table LIMIT 5 will also give the same result as above query.

查询SELECT * FROM表LIMIT 5也将提供与上述查询相同的结果。

If in that table there are fewer than 5 records then it will not fail but return whatever records are there.

如果在该表中少于5个记录,则它不会失败但返回那里的任何记录。

Query SELECT * FROM table LIMIT 6,5 will return record 7,8,9,10,11 as the index starts from 0.

查询SELECT * FROM表LIMIT 6,5将返回记录7,8,9,10,11,因为索引从0开始。

#2


http://dev.mysql.com/doc/refman/5.1/en/select.html

"The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be non-negative integer constants (except when using prepared statements). With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return."

“LIMIT子句可以用来约束SELECT语句返回的行数.LIMIT需要一个或两个数字参数,它们都必须是非负整数常量(使用预处理语句时除外)。有两个参数,第一个argument指定要返回的第一行的偏移量,第二行指定要返回的最大行数。“

So, to answer your question directly, it would return at most 5 rows.

因此,要直接回答您的问题,它最多会返回5行。

#3


The limit is, well, a limit, so it won't return more than that many rows. It can return less.

限制是一个限制,因此它不会返回超过那么多行。它可以减少回报。

#4


In query "select * from table limit 0,5" 0 does not specify the minimum records to return. It specifies the OFFSET. So when you say 0, if the query "select * from table" returns 10 records, "limit 0,5" will return the first 5. If you use "limit 5,5" it will return last 5 records.

在查询“select * from table limit 0,5”0中没有指定要返回的最小记录。它指定OFFSET。所以当你说0时,如果查询“select * from table”返回10条记录,“limit 0,5”将返回第5条。如果使用“limit 5,5”,它将返回最后5条记录。

If you have only 2 records, it will return two records. It will not return an error if you have no results. LIMIT is the maximum limit. Minimum can be anything, even 0 records.

如果您只有2条记录,它将返回两条记录。如果没有结果,它将不会返回错误。 LIMIT是最大限制。最小值可以是任何值,甚至是0记录。

"select * from table limit 5" is the same as "select * from table limit 0,5"

“select * from table limit 5”与“select * from table limit 0,5”相同