dette problem kaldes Relational Division
SELECT boy_id
FROM boys_chocolates
WHERE chocolate_id IN ('$string')
GROUP BY boy_id
HAVING COUNT(DISTINCT chocolate_id) = ? -- <<== number of chocolates specified
eksempel:
SELECT boy_id
FROM boys_chocolates
WHERE chocolate_id IN (1,2,3,4)
GROUP BY boy_id
HAVING COUNT(DISTINCT chocolate_id) = 4
dog hvis chocolate_id
er unik for hver boy_id
, DISTINCT
søgeord er valgfrit.
SELECT boy_id
FROM boys_chocolates
WHERE chocolate_id IN (1,2,3,4)
GROUP BY boy_id
HAVING COUNT(*) = 4
OPDATERING 1
...Jeg vil gerne vælge drenge fra bordet, der har præcis de chokolader, jeg nævner. Ikke mere ikke mindre...
SELECT boy_id
FROM boys_chocolates a
WHERE chocolate_id IN (1,2,3,4) AND
EXISTS
(
SELECT 1
FROM boys_chocolates b
WHERE a.boy_ID = b.boy_ID
GROUP BY boy_id
HAVING COUNT(DISTINCT chocolate_id) = 4
)
GROUP BY boy_id
HAVING COUNT(*) = 4