BSDnexus
3Dec/10Off

MySQL and PostgreSQL simple pagination

When using a databse you often want to limit the number of rows returned. In some cases you want to take segments, the second set of 10 rows for example. This is especially true when you wish to implement some form of pagination (like a blog). It seems that MySQL and PostgreSQL both support a feature to make life easier...

The commands you want are LIMIT and OFFSET. Like their namesake, one limits the number of rows returned while the other indicates the offset from the start. Both MySQL and PostgreSQL implement the features in the same way - an example being:

SELECT column1, column2
FROM tablename
LIMIT 10 OFFSET 10

Here we returned 10 rows from the database starting at row 11, esentially getting us entries 11-20

Tagged as: No Comments