I am looking for something like this but can\'t figure out the best way to write the query:
SELECT DISTINCT CategoryID FROM tbl_Categories c INNER JOIN
ma
you can do this:
SELECT CategoryID
FROM tbl_Categories c INNER JOIN
mappingTable mp ON c.CategoryID = mp.CategoryID INNER JOIN
SubCategories sc ON mp.SubCategoryID = sc.SubCategoryID
WHERE sc.SubcategoryID IN (234,245,645)
GROUP BY CategoryID
HAVING COUNT(sc.SubcategoryID)
= LEN(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE('234,245,645','0','')
, '1', '')
, '2', '')
, '3', '')
, '4', '')
, '5', '')
, '6', '')
, '7', '')
, '8', '')
, '9', '')
, ' ', '')) + 1
SELECT CategoryID
FROM tbl_Categories c INNER JOIN
mappingTable mp ON c.CategoryID = mp.CategoryID INNER JOIN
SubCategories sc ON mp.SubCategoryID = sc.SubCategoryID
WHERE sc.SubcategoryID IN (234,245,645)
GROUP BY CategoryID
HAVING COUNT(sc.SubcategoryID)
= (SELECT COUNT(DISTINCT SubcategoryID)
FROM SubCategories
WHERE SubcategoryID IN (234,245,645))