Here's another approach that avoids programmatically adding subqueries to your WHERE clause:
Query:
SELECT count(person.id) AS numPeople, person
FROM Person as person
INNER JOIN
person.memberships AS mships
WITH mships.group.id IN (:groupIds)
GROUP BY person.id
HAVING COUNT(person.id) = (:numOfGroupIds)
And some sample values:
[
groupIds: [8,9,439,86843]
numOfGroupIds: 4
]
Part of this query before GROUP BY captures all the people that match any of the groups. Then, grouping by person and checking the number of results is equal to the number of groups in the list, you can check that this person is a member of all these groups.
source
share