Lets say I have the following tables:
TABLE: topic
COLUMNs: topicId
TABLE: topicPermissions
COLUMNs: topicId, teamId
I want to write a query that pulls 10 unique topic id’s and all of their associated permissions rows. Each topic id can have multiple topicPermission rows assigned to it, but I only want 10 distinct topic id’s.
SELECT topic.topicId, topicPermission.teamId FROM topic LEFT JOIN topicPermissions ON topicId LIMIT 10
Obviously this doesn’t work because it limits it to 10 rows. Any help appreciated.
Just to clarify further: Another way to accomplish this would be to keep pulling rows from topic permissions until topic permissions has 10 distinct topic id’s. Is that possible?
Thanks.
This might work:
Hope it helps!
EDIT 1:
Here is a test run for the above query.
Table “topic” has 13 records:
Table “topicPermissions” has 4 entries for topicId = 1 and 2 entries for topicId = 5:
The resultant:
As I understand from your question, the query gives you 10 distinct topic ids and all topicPermissions for each topic id. No?
Please note that if you need to pick specific topic ids, you may change the derived table query below to read the desired topics: