字段中的歧义列,但当我加前缀时,它表示“未知”

时间:2014-06-28 03:27:41

标签: mysql

我有以下查询

SELECT table1.user_id as uID, table1.password AS pass, table1.libID AS user_id, table2.firstname, lockout.loginAttempts, lockout.lastAttemptTime
    FROM( SELECT 'johnDoe' user ) u 
    LEFT JOIN table1 a 
    ON u.user = a.username 
    INNER JOIN table2
    ON table1.libID = table2.user_id 
    LEFT JOIN 
    (
        SELECT attemptedUsername, loginAttempts, lastAttemptTime
        FROM lockout 
        WHERE accountType = 'public' 
    ) l 
    ON u.user = l.attemptedUsername

最初,我没有在字段列表中使用表名前缀列名,但由于table1和table都有名为user_id的列 - mysql给了我“模糊不清”的字样。 user_id的错误。

在添加前缀后,我收到了错误

Unknown column 'songbookdbpal_users.user_id' in 'field list' 

这个专栏肯定存在 - 我在语法中绊倒了吗?

感谢您的时间和帮助。

1 个答案:

答案 0 :(得分:2)

由于您为table1提供了别名,因此必须在查询的其余部分中使用该别名:

SELECT a.user_id as uID, a.password AS pass, a.libID AS user_id, 
        table2.firstname, lockout.loginAttempts, lockout.lastAttemptTime
FROM( SELECT 'johnDoe' user ) u 
LEFT JOIN table1 a 
ON u.user = a.username 
INNER JOIN table2
ON a.libID = table2.user_id 
LEFT JOIN 
(
    SELECT attemptedUsername, loginAttempts, lastAttemptTime
    FROM lockout 
    WHERE accountType = 'public' 
) l 
ON u.user = l.attemptedUsername