如何让GROUP CONCAT运算符中的WHERE依赖于查询中的另一列?在这种情况下,需要发生的是score_list
的值取自相关候选人的分数。在MySQL中,我能够通过使用别名(SELECT candidate_id作为id .... WHERE scores.candidate_id = id ...)来使用它,在SQLite中我得到“没有这样的列”。
SELECT candidate_id,
first_name || ' ' || last_name AS 'name',
(
SELECT GROUP_CONCAT(score)
FROM scores
WHERE scores.candidate_id = *NEEDS TO BE CANDIDATE_ID*
AND scores.category_id = 1
ORDER BY scores.judge_id
) AS score_list
FROM scores
JOIN candidates
ON candidates.id = scores.candidate_id
答案 0 :(得分:0)
尝试对表格进行别名处理。
SELECT oute.candidate_id,
oute.first_name || ' ' || oute.last_name AS 'name',
(
SELECT GROUP_CONCAT(inne.score)
FROM scores inne
WHERE inne.candidate_id = oute.candidate_id
AND inne.category_id = 1
ORDER BY inne.judge_id
) AS score_list
FROM scores oute
JOIN candidates
ON candidates.id = oute.candidate_id