MySQL LIMIT

How to limit the amount of data outputted by a query.

Using LIMIT will specify how many records to return in a query. If there is only one number after the word LIMIT statement then that number will tell you how many records to return. If there are two numbers after the LIMIT statement then the first number refers to how many records to return and the second refers to how many records to skip before outputting records. If you have a database with 100 records and use the query "SELECT * from tablename LIMIT 5, 20" it will output 5 records and skip the first 20. So it will show records # 21-25.

SELECT columns FROM tablename WHERE columnname = 'value' ORDER BY 'column' DESC

The following examples will use the following database.



LIMIT

This query sorts by age and the shows only the first record. In plain language, it shows the youngest person in the database.

MySQL Code

$query="SELECT firstname, lastname, age FROM users ORDER BY age LIMIT 1";




Results


Martin Gross 16



LIMIT - starting with record 'x'

In this example, 2 records will be returned starting with the third.

MySQL Code

$query="SELECT firstname, lastname, age FROM users ORDER BY age LIMIT 2, 2";




Results


Eric Friedman 27
Brian Grossman 31




 
Bookmark this page:
   
 
Shared Hosts
Lunarpages.com Web Hosting

Domain Registrars
PPC Networks
Logos
Affiliate Networks
Resources