选择查询使用3个表

时间:2017-05-01 05:31:08

标签: mysql sql syntax-error

我有3张桌子


person_id
username_email

language_skill
person_id
language_id
language_teaching
language_learning

语言
language_id
language_name

我想选择教用英语的用户的username_email。
我不知道如何使用连接来构造我的select语句。

SELECT person.person_id, person.username_email FROM language_skill 
INNER JOIN person ON language_skill.person_id = person.person_id
INNER JOIN language_skill.language_id = language.language_id
WHERE language_skill.language_teaching = 1 AND language.language_name = English

我收到的错误是

  

1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在'.language_id WHERE language_skill.language_teaching = 1 AND language.language_na'第3行附近使用正确的语法

4 个答案:

答案 0 :(得分:4)

在您的查询中,您错过了第二个on,并且应将英语放入'括号中

尝试:

SELECT person.person_id, person.username_email FROM language_skill 
INNER JOIN person ON language_skill.person_id = person.person_id
INNER JOIN language on language_skill.language_id = language.language_id
WHERE language_skill.language_teaching = 1 AND language.language_name = 'English'

答案 1 :(得分:4)

您还可以为每个表使用别名:

SELECT b.person_id, b.username_email FROM language_skill a
INNER JOIN person b ON a.person_id = b.person_id
INNER JOIN language c ON a.language_id = c.language_id
WHERE a.language_teaching = 1 AND c.language_name = 'English';

答案 2 :(得分:2)

第二次加入不完整。 语法是

[type] JOIN [table] ON [condition]

校正的

SELECT
    person.person_id, 
    person.username_email 
FROM language_skill 
    INNER JOIN person ON language_skill.person_id = person.person_id
    INNER JOIN language ON  language_skill.language_id = language.language_id
WHERE language_skill.language_teaching = 1 AND language.language_name = 'English'

答案 3 :(得分:1)

尝试简单加入

SELECT P.`person_id`, p.`username_email` FROM PERSON P , language_skill LS ,
language L WHERE P.`person_id` = LS.`person_id` AND L.language_name  = 
'ENGLISH' AND L.language_id =LS.`language_id`