简单的SQL连接查询(我认为)

时间:2014-02-09 14:04:44

标签: mysql sql

任何人都可以告诉我为什么会出现错误:当我使用FULL JOIN时,'where子句'中的未知列'cdu_user_progress.uid',但是当我使用JOIN时却没有?我正在使用MySQL ......

SELECT 
    cdu_lessons.*, 
    SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN game_id = 0 THEN score END ORDER BY date), ',', 1) AS game0_first,
    MAX(CASE WHEN game_id = 0 THEN score END) AS game0_max,
    SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN game_id = 1 THEN score END ORDER BY date), ',', 1) AS game1_first,
    MAX(CASE WHEN game_id = 1 THEN score END) AS game1_max,
    SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN game_id = 2 THEN score END ORDER BY date), ',', 1) AS game2_first,
    MAX(CASE WHEN game_id = 2 THEN score END) AS game2_max,
    SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN game_id = 3 THEN score END ORDER BY date), ',', 1) AS game3_first,
    MAX(CASE WHEN game_id = 3 THEN score END) AS game3_max,
    SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN game_id = 4 THEN score END ORDER BY date), ',', 1) AS game4_first,
    MAX(CASE WHEN game_id = 4 THEN score END) AS game4_max,
    SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN game_id = 5 THEN score END ORDER BY date), ',', 1) AS game5_first,
    MAX(CASE WHEN game_id = 5 THEN score END) AS game5_max     
FROM cdu_user_progress 
FULL JOIN cdu_lessons ON cdu_lessons.id = cdu_user_progress.lesson_id 
WHERE cdu_user_progress.uid = 145 
GROUP BY lesson_id

2 个答案:

答案 0 :(得分:1)

mysql不支持

FULL JOIN,至少不是直接支持,因此FROM cdu_user_progress FULL JOIN...被解释为FROM cdu_user_progress as FULL。 实际上,它用别名FULL替换表名。

答案 1 :(得分:0)

MySQL缺乏对FULL OUTER JOIN的支持。

因此,如果您想在MySQL上模拟完全加入,请查看here

通常建议的解决方法如下所示:

   SELECT 
        cdu_lessons.*, 
        SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN game_id = 0 THEN score END ORDER BY date), ',', 1) AS game0_first,
        MAX(CASE WHEN game_id = 0 THEN score END) AS game0_max,
        SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN game_id = 1 THEN score END ORDER BY date), ',', 1) AS game1_first,
        MAX(CASE WHEN game_id = 1 THEN score END) AS game1_max,
        SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN game_id = 2 THEN score END ORDER BY date), ',', 1) AS game2_first,
        MAX(CASE WHEN game_id = 2 THEN score END) AS game2_max,
        SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN game_id = 3 THEN score END ORDER BY date), ',', 1) AS game3_first,
        MAX(CASE WHEN game_id = 3 THEN score END) AS game3_max,
        SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN game_id = 4 THEN score END ORDER BY date), ',', 1) AS game4_first,
        MAX(CASE WHEN game_id = 4 THEN score END) AS game4_max,
        SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN game_id = 5 THEN score END ORDER BY date), ',', 1) AS game5_first,
        MAX(CASE WHEN game_id = 5 THEN score END) AS game5_max     
    FROM cdu_user_progress 
    LEFT JOIN cdu_lessons ON cdu_lessons.id = cdu_user_progress.lesson_id 
    WHERE cdu_user_progress.uid = 145 
    GROUP BY lesson_id

Union ALL

    SELECT 
        cdu_lessons.*, 
        SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN game_id = 0 THEN score END ORDER BY date), ',', 1) AS game0_first,
        MAX(CASE WHEN game_id = 0 THEN score END) AS game0_max,
        SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN game_id = 1 THEN score END ORDER BY date), ',', 1) AS game1_first,
        MAX(CASE WHEN game_id = 1 THEN score END) AS game1_max,
        SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN game_id = 2 THEN score END ORDER BY date), ',', 1) AS game2_first,
        MAX(CASE WHEN game_id = 2 THEN score END) AS game2_max,
        SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN game_id = 3 THEN score END ORDER BY date), ',', 1) AS game3_first,
        MAX(CASE WHEN game_id = 3 THEN score END) AS game3_max,
        SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN game_id = 4 THEN score END ORDER BY date), ',', 1) AS game4_first,
        MAX(CASE WHEN game_id = 4 THEN score END) AS game4_max,
        SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN game_id = 5 THEN score END ORDER BY date), ',', 1) AS game5_first,
        MAX(CASE WHEN game_id = 5 THEN score END) AS game5_max     
    FROM cdu_user_progress 
    RIGHT JOIN cdu_lessons ON cdu_lessons.id = cdu_user_progress.lesson_id 
    WHERE cdu_user_progress.uid = 145 
    GROUP BY lesson_id