我有这个问题:
SELECT
course_category.id AS languageId,
course_category.code AS languageCode,
course_category.name AS languageName,
(
SELECT
SUM(gradebook_result.score)
FROM
gradebook_result
JOIN
gradebook_evaluation ON gradebook_evaluation.id=gradebook_result.evaluation_id
JOIN
gradebook_category ON gradebook_category.id=gradebook_evaluation.category_id
WHERE
gradebook_category.course_code=course_category.code
) AS languageWordsTranslated
FROM
course_category
WHERE
course_category.code != 'GEN'
ORDER BY
name
ASC
问题发生在嵌套的SELECT中,我在SELECT行上引用了course_category表时出错:
WHERE
gradebook_category.course_code=course_category.code
给出错误:
Unknown column 'course_category.code' in 'where clause'
我之前使用其他项目完成了此查询,唯一的区别是此内容已加入其中。有什么想法吗?
编辑:我删除了连接并硬编码:
WHERE
course_category.code = 'ARA'
似乎连接混乱,任何方法来解决这个问题?
答案 0 :(得分:2)
尝试以不同的方式使用子查询并加入它:
SELECT
course_category.id AS languageId,
course_category.code AS languageCode,
course_category.name AS languageName,
t.total AS languageWordsTranslated
FROM
course_category
JOIN (
SELECT gradebook_category.course_code, SUM(gradebook_result.score) as total
FROM gradebook_result
JOIN gradebook_evaluation ON gradebook_evaluation.id=gradebook_result.evaluation_id
JOIN gradebook_category ON gradebook_category.id=gradebook_evaluation.category_id
GROUP BY gradebook_category.course_code
) t ON t.course_code = course_category.code
WHERE
course_category.code != 'GEN'
ORDER BY
name
ASC