Skip to content

Instantly share code, notes, and snippets.

@xtrasmal
Created May 26, 2013 20:16
Show Gist options
  • Select an option

  • Save xtrasmal/5653891 to your computer and use it in GitHub Desktop.

Select an option

Save xtrasmal/5653891 to your computer and use it in GitHub Desktop.
MySQL Relational division
SELECT Products.*
FROM Products
JOIN ProductTags ON Products.id = ProductTags.product_id
WHERE ProductTags.tag_id IN (1,2,3)
GROUP BY Products.id
HAVING COUNT(DISTINCT ProductTags.tag_id) = 3
/*
Assuming that there is a unique
constraint on product_id,tag_id you
don't need the DISTINCT
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment