Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL limit range

Tags:

SELECT name FROM mydb ORDER BY score DESC LIMIT 10; 

The query above will return the first 10 ranks.

How to modify the LIMIT, or maybe is there another syntax to query the 10th rank through the 20th rank?

like image 462
theHack Avatar asked Apr 23 '11 22:04

theHack


People also ask

How do I LIMIT data in MySQL?

Limit Data Selections From a MySQL Database Assume we wish to select all records from 1 - 30 (inclusive) from a table called "Orders". The SQL query would then look like this: $sql = "SELECT * FROM Orders LIMIT 30"; When the SQL query above is run, it will return the first 30 records.

How do you set a range of limits in SQL?

The SQL SELECT LIMIT statement is used to retrieve records from one or more tables in a database and limit the number of records returned based on a limit value. TIP: SELECT LIMIT is not supported in all SQL databases. For databases such as SQL Server or MSAccess, use the SELECT TOP statement to limit your results.

How do I LIMIT the number of rows in MySQL?

In MySQL the LIMIT clause is used with the SELECT statement to restrict the number of rows in the result set. The Limit Clause accepts one or two arguments which are offset and count. The value of both the parameters can be zero or positive integers.

How do you set a LIMIT in a query?

The limit keyword is used to limit the number of rows returned in a query result. “SELECT {fieldname(s) | *} FROM tableName(s)” is the SELECT statement containing the fields that we would like to return in our query. “[WHERE condition]” is optional but when supplied, can be used to specify a filter on the result set.


2 Answers

You should use:

SELECT name FROM mydb ORDER BY score DESC LIMIT 10,10; 

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

The two arguments 10,10 are (Offset, Limit) so this will retrieve rows 11-20.
9,11 Would be required to grab the 10th - 20th rank.

like image 142
James C Avatar answered Oct 03 '22 08:10

James C


Use offset to clarify the query.

SELECT name FROM mydb ORDER BY score DESC LIMIT 10 OFFSET 10 
like image 29
jotapdiez Avatar answered Oct 03 '22 08:10

jotapdiez