Created
May 26, 2013 20:16
-
-
Save xtrasmal/5653891 to your computer and use it in GitHub Desktop.
MySQL Relational division
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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