MySQL Unknown列u.id in on子句但列存在

时间:2014-03-14 10:12:44

标签: mysql sql left-join

我有这张桌子:

users {id = int, name = varchar, pwd = char}
company {id = int, token = char, name = varchar}
user_company {id = int, id_usr = int, id_company = int, name_usr = varchar}

我正在尝试从用户那里获取pwd并使用user_company中的令牌X查明用户是否在公司中

当我使用此查询时

SELECT u.pwd,h.name_usr
FROM users u, company c
    LEFT JOIN users_company h ON c.id = h.id_company AND u.id = h.id_usr
WHERE u.user_name = 'user@domain.com'
    AND c.token = 'f30ea71e7a9d9f0a6710bb46537c0bde'
LIMIT 1;

我继续获得'Unknown column u.id in on clause',尽管u.id存在。我究竟做错了什么?感谢

2 个答案:

答案 0 :(得分:0)

    SELECT u.pwd,h.nombre_usr
    FROM company c LEFT JOIN users_company h ON c.id = h.id_company 
    LeFt join users u  on  u.id = h.id_usr
    WHERE u.user_name = 'user@domain.com'
    AND c.token = 'f30ea71e7a9d9f0a6710bb46537c0bde'
    LIMIT 1;

错误是因为加入查询。写它的方式是错误的。我们不能同时使用单个表连接两个表。

答案 1 :(得分:-1)

我有解决方案。谢谢大家的帮助。

SELECT u.pwd,h.name_usr
FROM users u
    LEFT JOIN company c ON c.token = 'f30ea71e7a9d9f0a6710bb46537c0bde'
    LEFT JOIN users_company h ON c.id = h.id_company AND u.id = h.id_usr
WHERE u.user_name = 'user@domain.com'
LIMIT 1;