I have a table :
Person Language
6 1
6 2
6 3
7 1
7 2
I would like to select the person who speaks the language 1 AND 2 AND 3 (person 6)
I coded this query, but I don't get the right result :
SELECT guides.id, guides.nom, guides.prenom, langues.langue
FROM guides
JOIN guides_has_langues ON guides_has_langues.guides_id = guides.id
JOIN langues ON guides_has_langues.langues_id = langues.id
WHERE guides_has_langues.langues_id = 1 AND guides_has_langues.langues_id = 2 AND guides_has_langues.langues_id = 3
I think that this query select field that are 1 AND 2 AND 3, is it right ?
It's not my goal.
Select all entries for languages 1, 2 and 3. Then group by guide and stay with those having all three languages (using the HAVING clause). To show the languages use the function your dbms offers to concatenate them (e.g. GROUP_CONCAT for MySQL).
SELECT guides.id, guides.nom, guides.prenom, GROUP_CONCAT(langues.langue)
FROM guides
JOIN guides_has_langues ON guides_has_langues.guides_id = guides.id
JOIN langues ON guides_has_langues.langues_id = langues.id
WHERE guides_has_langues.langues_id IN (1,2,3)
GROUP BY guides.id, guides.nom, guides.prenom
HAVING COUNT(DISTINCT guides_has_langues.langues_id) = 3;
(If you want one line per guide and language this will be a little more complicated, because you need the raw entires plus the aggregation information on how many languages the guide speaks.)