Skip to content

Instantly share code, notes, and snippets.

@robertscherer
Created September 18, 2015 13:00
Show Gist options
  • Save robertscherer/f7140483143f6af8cc3e to your computer and use it in GitHub Desktop.
Save robertscherer/f7140483143f6af8cc3e to your computer and use it in GitHub Desktop.
SELECT s.* FROM services s
INNER JOIN (
SELECT st.service_id FROM services_typetags st
INNER JOIN services s ON s.id = st.service_id
INNER JOIN typetags t ON t.id = st.typetag_id
WHERE t.id IN (8, 9, 11)
GROUP BY st.service_id
HAVING COUNT(st.service_id) = 3
) ss
ON s.id = ss.service_id
@robertscherer
Copy link
Author

Here we want to filter using the services_typetags n:m relation and have all services returned, which have at least the typetags 8, 9 and 11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment