基本上我有以下查询:
SELECT DISTINCT users.ID, users.name
FROM users
INNER JOIN usersSkills ON users.ID = usersSkills.userID
INNER JOIN usersLanguages ON users.ID = usersLanguages.userID
WHERE activated = "1"
AND type = "GRADUATE"
AND usersSkills.skillID IN(2, 21)
AND usersLanguages.languageID IN(2, 22)
我需要将技能ID和languageID与IN()函数中传递的每个参数匹配,如果找到任一参数,它将在每个参数上提供结果。
所以基本上我只想在技能表上找到2和21的技能ID以及语言的相同想法时才能看到结果......
答案 0 :(得分:2)
使用GROUP BY(这也不需要SELECT DISTINCT)来获取聚合计数和HAVING子句,以确保返回的不同元素的数量与您要搜索的元素数量相匹配。
SELECT users.ID, users.name
FROM users
INNER JOIN usersSkills ON users.ID = usersSkills.userID
INNER JOIN usersLanguages ON users.ID = usersLanguages.userID
WHERE activated = "1"
AND type = "GRADUATE"
AND usersSkills.skillID IN(2, 21)
AND usersLanguages.languageID IN(2, 22)
GROUP BY users.ID, users.name
HAVING COUNT(DISTINCT usersSkills.skillID) = 2
AND COUNT(DISTINCT usersLanguages.languageID) = 2