我正在努力让所有使用英语和法语的用户根据上面的架构。我怎样才能做到这一点?
我尝试过类似的事情:
SELECT * FROM User
INNER JOIN UserLanguage on User.idUser = UserLanguage.idUser
INNER JOIN Language on UserLanguage.idLanguage = Language.idLanguage
WHERE Language.name = "FR" AND Language.name = "EN"
答案 0 :(得分:3)
我正在使用子查询来计算每个用户说出的语言数量(FR或EN)。然后它返回所有使用这两种语言的用户的所有id。外部查询返回每个用户的所有列:
SELECT Users.*
FROM Users
WHERE idUser IN (
SELECT UserLanguage.idUser
FROM
UserLanguage INNER JOIN Language
ON UserLanguage.idLanguage = Language.idLanguage
WHERE
Language.name IN ("FR", "EN")
GROUP BY
UserLanguage.idUser
HAVING
COUNT(DISTINCT Language.name)=2
)
答案 1 :(得分:2)
从
更改条件WHERE Language.name = "FR" AND Language.name = "EN"
到
WHERE (Language.name = "FR" OR Language.name = "EN")
你不应该在数据库中有一个条目,它对一个字段有两个值,但如果你使用" OR"运算符,如果任一值相等,则应选择该条目。
答案 2 :(得分:2)
SELECT *
FROM User
WHERE 2 = (SELECT COUNT(*)
FROM UserLanguage
INNER JOIN Language
ON UserLanguage.idLanguage = Language.idLanguage
AND (Language.name = 'FR' OR Language.name = 'EN')
WHERE User.idUser = UserLanguage.idUser)
答案 3 :(得分:1)
总是尝试从圆圈的“中心”的角度来看待连接。在这种情况下,您尝试通过UserLanguage将“用户”加入“语言”,因此您的FROM表必须是“UserLanguage”。
所以基本上你在看:
SELECT * FROM UserLanguage
INNER JOIN User ON Userlanguage.idUser = User.idUser
INNER JOIN Language on Userlanguage.idLanguage = Language.idLanguage
WHERE (Language.Name = 'EN' OR Language.Name = 'FR');
答案 4 :(得分:1)
SELECT * FROM User
INNER JOIN UserLanguage on User.idUser = UserLanguage.idUser
INNER JOIN Language on UserLanguage.idLanguage = Language.idLanguage
WHERE Language.name = "FR" AND Language.name = "EN"
您只需要更改
即可 WHERE Language.name = "FR" AND Language.name = "EN"
到
WHERE (Language.name = "FR" OR Language.name = "EN")
答案 5 :(得分:0)
SELECT *
FROM Users u INNER JOIN UserLanguage ul
ON u.idUser = ul.idUser JOIN Language l
ON ul.idLanguage = l.idLanguage
WHERE l.LanguageName IN ('EN', 'FR')
ORDER BY u.idUser
答案 6 :(得分:0)
您必须将UserLanguage表连接到自身,例如:
SELECT T1.idUser
FROM UserLanguage AS T1
INNER JOIN UserLanguage AS T2
ON T1.idUser=T2.idUser
WHERE T1.idLanguage='EN'
AND T2.idLanguage='FR'