MySQL未知列问题

时间:2012-09-07 23:38:20

标签: mysql sql database

这是我的数据库结构

https://docs.google.com/open?id=0B9ExyO6ktYcOenZ1WlBwdlY2R3c

我的SQL查询看起来像那样

SELECT
    u.fullname,
    a.id,
    a.content,
    a.addDT,
    a.`right`,
    acr.score,
    acr.checkDT
FROM
    answers a,
    users u
LEFT JOIN `answer_chk_results` acr ON acr.aid = a.id
WHERE
    a.qid = 7
AND u.id = a.uid
GROUP BY
    a.`right` DESC

收到错误消息

[Err] 1054 - Unknown column 'a.id' in 'on clause'

我很确定a.id列存在

我错过了什么?

1 个答案:

答案 0 :(得分:1)

您不能将连接语法与逗号分隔的表语法混合使用。如果要离开连接,则需要对其他两个表使用内连接。

SELECT
    u.fullname,
    a.id,
    a.content,
    a.addDT,
    a.`right`,
    acr.score,
    acr.checkDT
FROM
    answers a
    INNER JOIN users u ON u.id = a.uid
LEFT JOIN `answer_chk_results` acr ON acr.aid = a.id
WHERE
    a.qid = 7
GROUP BY
    a.`right` DESC

可能也可以使用,但不保证:

SELECT
    u.fullname,
    a.id,
    a.content,
    a.addDT,
    a.`right`,
    acr.score,
    acr.checkDT
FROM
    (answers a,
    users u)
LEFT JOIN `answer_chk_results` acr ON acr.aid = a.id
WHERE
    a.qid = 7
AND u.id = a.uid
GROUP BY
    a.`right` DESC