Correct pagination in participant selection

I have a SQL statement

select * 
from users u left join files f
on u.id = f.user_id
where f.mime_type = 'jpg'
order by u.join_date desc
limit 10 offset 10

1-N Ratio: A user can have many files.

This effectively selects the second 10-element page.

The problem is that this query limits / cancels the joined table, but I want to limit / offset different rows from the first table ( users).

How? I am targeting PostgreSQL and HSQLDB

+5
source share
1 answer

First you need to limit the selection in the external table and then join the dependent table to the result.

select * from (select * from users limit 10 offset 10)  as u
left join files f
   on u.id = f.user_id
+8
source

All Articles