获取错误消息[错误] 1060 - 重复列名'id'
SELECT c.id, c.name, COUNT(all.id) 'All',
COUNT(unanswered.id) 'Unanswered',
COUNT(unchecked.id) 'Unchecked'
FROM courses c
INNER JOIN courses-lessons cl ON c.id = cl.cid
INNER JOIN questions all ON cl.id = all.lid
INNER JOIN
(
SELECT *
FROM questions
WHERE id NOT IN (SELECT qid FROM answers)
) unanswered ON cl.id = unchecked.lid
INNER JOIN
(
SELECT *
FROM questions q
INNER JOIN
(
SELECT *
FROM answers
WHERE id NOT IN (SELECT aid FROM answer_chk_results)
) a ON q.id = a.qid
) unchecked ON cl.id = unchecked.lid
GROUP BY c.id, c.name
我错过了什么?找不到什么错误。
数据库结构
https://docs.google.com/open?id=0B9ExyO6ktYcOenZ1WlBwdlY2R3c
答案 0 :(得分:0)
您的问题和答案表都有id列。当你做SELECT *时,你会得到两个id列。尝试在SELECT中指定所需的列,而不是使用*
请点击此处查看类似问题:SQL Duplicate column name error
答案 1 :(得分:0)
不确定问题是否属于您的问题,但您不应将表别名为“all”,因为它是MySQL关键字。
答案 2 :(得分:0)
这很可能是你的查询的这一部分:
SELECT *
FROM questions q
INNER JOIN
(
SELECT *
FROM answers
WHERE id NOT IN (SELECT aid FROM answer_chk_results)
) a ON q.id = a.qid
外部查询将有两个名为id
的字段,您应该指定所需的id
字段。此外,您应该调出所需的字段,而不是使用SELECT *
。
ALL
也是reserved word,我会考虑更改别名或在其周围使用反引号`
。
根据您的表格结构,它看起来像你想要的那样:
SELECT c.id,
c.name,
COUNT(allq.id) 'All',
COUNT(unanswered.id) 'Unanswered',
COUNT(unchecked.id) 'Unchecked'
FROM courses c
INNER JOIN courses-lessons cl
ON c.id = cl.cid
INNER JOIN questions allq
ON cl.id = allq.lid
INNER JOIN
(
SELECT q.id, a.qid, q.lid
FROM questions q
INNER JOIN
(
SELECT id, qid
FROM answers
WHERE id NOT IN (SELECT aid FROM answer_chk_results)
) a
ON q.id = a.qid
) unchecked
ON cl.id = unchecked.lid
INNER JOIN
(
SELECT id
FROM questions
WHERE id NOT IN (SELECT qid FROM answers)
) unanswered
ON cl.id = unchecked.lid
GROUP BY c.id, c.name