Skip to content
Advertisement

LIMIT and OFFSET returning repeating results

There seems to be a bug in my query that causes results to repeat when I increment the OFFSET value. It is supposed to show 20 results per page. Only 3 results match what my query is looking for, but for some reason, some of these results repeat themselves even though they should only appear on the first page of the query results (see below for example).

the results of this query are …

If I make OFFSET = 2 ie OFFSET 2 the results are …

the results of this query are

… Even though these results should only appear when the offset = 1.

If I make OFFSET = 3 ie OFFSET 3 the results are…

… Even though these results should only appear when the offset = 1.

Any idea on how to fix this strange behavior? If not, do you see any bugs in my queries?

Additional info …

users table

subscribers table

Advertisement

Answer

According to PostgreSQL documents offset means OFFSET says to skip that many rows before beginning to return rows.

In your query, you add LIMIT 20 OFFSET 2 this means skip 2 rows from 20 existing rows and two first rows have been skipped and showing duplicate rows of the previous page.

You should use this formula for calculate limit and offset:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement