2013-09-23 86 views
0

如何獲取GROUP CONCAT操作符中的WHERE依賴於查詢中的另一列?在這種情況下,需要發生的是score_list的值取自相關候選人的分數。在MySQL中,我可以通過使用別名(SELECT候選id作爲id .... WHERE scores.candidate_id = id ...)來使用它,在SQLite中,我得到了「沒有這樣的列」。SQLite SELECT查詢 - 基於另一列值的GROUP CONCAT列

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

嘗試別名表。

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