MySQL’s LIMIT
clause is used to specify the number of records to return. It is particularly useful for implementing pagination or handling large datasets, as returning too many records can affect performance.
For instance, to select records from 1 to 30 (inclusive) from a table named “Orders”, the SQL query would be:
$sql = “SELECT * FROM Orders LIMIT 30”; |
When the SQL query above is executed, it returns the first 30 records.
To select records 16 through 25 (inclusive), MySQL offers a solution using OFFSET
.
The SQL query below specifies “return only 10 records, starting from record 16 (OFFSET 15)”:
$sql = “SELECT * FROM Orders LIMIT 10 OFFSET 15”; |
You can also use a more concise syntax to achieve the same result:
$sql = “SELECT * FROM Orders LIMIT 15, 10”; |
Note that when using a comma, the numbers are reversed.