tags:

views:

1634

answers:

4

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?


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

A: 

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

David Zaslavsky
+6  A: 

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."

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

yawmark
+2  A: 

Query select * from table limit 0,5 will return 5 records starting from the first record.

Query select * from table limit 5 will also give the same result as above query.

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

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

Bhushan
as for yawmark i've read the mysql documentation but found it very technical so i accepted this answer which explains it more for laymans
lock
A: 

Hi,

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.

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.

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

Alec Smart