Skip to content

Instantly share code, notes, and snippets.

@jonahgeorge
Last active August 29, 2015 14:25
Show Gist options
  • Save jonahgeorge/5134b72cd3d7d43e5758 to your computer and use it in GitHub Desktop.
Save jonahgeorge/5134b72cd3d7d43e5758 to your computer and use it in GitHub Desktop.
SELECT "resources".*
FROM "resources"
INNER JOIN "resources_tags" ON "resources_tags"."resource_id" = "resources"."id"
INNER JOIN "tags" ON "tags"."id" = "resources_tags"."tag_id"
WHERE ("tags"."id" IN (4,16)
AND (SELECT count(*) FROM "tags" WHERE "tags"."id" IN(4,16)) = 2)
GROUP BY "resources"."id"
ORDER BY "resources"."name" ASC
-- Ryan's new method
SELECT DISTINCT(ti.resource) FROM adw_stg.tag_ids ti
JOIN (SELECT resource, count(id) FROM adw_stg.tag_ids
WHERE(id IN(4,16))
GROUP BY resource) sq
ON(sq.resource = ti.resource
AND sq.count = 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment