Reputation: 1070
I have a table that will have data like this
PRODUCTID | GENREID
1 | 5
1 | 6
1 | 8
2 | 5
2 | 10
3 | 6
And I'm trying to write a statement to where if I provide the numbers 5, 6 and 8, it will only return to me ProductID '1'. My initial thought was to write something like this, but I'm starting to confuse myself as I'm not a SQL ninja :
select
p1.productid
from
dbo.ProductGenre p1
inner join
dbo.ProductGenre p2 on p2.GenreID = p1.GenreID
where
p1.GenreID = 5
Upvotes: 0
Views: 45
Reputation: 204746
SELECT productid
FROM dbo.productgenre
WHERE genreid IN ( 5, 6, 8 )
GROUP BY productid
HAVING Count(DISTINCT genreid) = 3
Upvotes: 3