This tutorial explains how to use limit clause within MySQL query to fetch records. The MySQL LIMIT clause is used to limit the number of records returned from MySQL query by using value passed with LIMIT clause. Limit clause is used with the SELECT statement to constrain the number of records returned in result set.
Limit clause takes one or two numeric arguments, The values for both arguments are only positive integers.
Syntax1 :
SELECT column_names
FROM table
LIMIT offset, count
Syntax2 :
SELECT column_names
FROM table
LIMIT count
Description :
offset : Defines the offset of first row to return.(default value is 0)
count : Defines the maximum number of records to return.
Note : If only single argument is passed in query than offset used its default value.
Example1 :
SELECT *
FROM `users`
LIMIT 5
Example2 :
SELECT *
FROM `users`
LIMIT 0, 5
In the above both examples, Both queries returns the first five records from users table.
Get Records with in Range
Example :
SELECT *
FROM `users`
LIMIT 10, 5
In the above example query returns the rows between the range starting from 10th row upto 14th row. In the query offset is set with value 10 and count value is 5. So the query start fetching records from 10th row and fetch 5 rows in result set.
0 Comment(s)