I have a documents permissions table, and I wish to select a page of documents for a user.
Thing is, a user can be permitted to a document through more than one group, thus we might have more than X records for X documents
for the user.
I tried to use a subquery for paging, but MySQL didn't like LIMIT in the subquery
.
I'm facing two major options:
SELECT perm.* FROM (SELECT DISTINCT document_id FROM permissions WHERE .... LIMIT...) ids INNER JOIN (SELECT fields FROM permissions WHERE ....) perms ON ids.document_id = perms.document_id
Using Java for the join - first read the document ids, and then use them in a query like:
SELECT fields FROM permissions WHERE .... AND document_id IN([specific document_ids previously read])
What would be more efficient?