发生数据库错误错误号:1066 不是唯一的表/别名:[重复]

Posted

技术标签:

【中文标题】发生数据库错误错误号:1066 不是唯一的表/别名:[重复]【英文标题】:A Database Error Occurred Error Number: 1066 Not unique table/alias: [duplicate] 【发布时间】:2016-10-04 07:54:37 【问题描述】:

嘿,我正在构建非常复杂的查询,我不知道它是否被计算为复杂,但由于我是 SQL 新手,所以这非常复杂

错误信息:

发生数据库错误 错误号:1066 不是唯一的表/别名:'bb'

 SELECT cl.*,
            (CASE  
                WHEN id_student IS NOT NULL THEN 'BOOKED' ELSE 
                (
                    CASE 
                    WHEN (max_count - student_count) > 0 THEN 'AVAILABLE' ELSE 'FULL'
                    END
                )
            END) AS course_status        
        FROM (    
                SELECT         aa.*, bb.id_student, cc.max_count, dd.student_count
                FROM        (
                                    SELECT c.id_course, c.id_study, c.id_level, c.id_teacher, c.course_date, 
                                s.study_name,
                                t.teacher_name,
                                t.teacher_picture
                                FROM tbl_course c
                                LEFT JOIN tbl_study s ON s.id_study = c.id_study
                                LEFT JOIN tbl_teacher t ON t.id_teacher = c.id_teacher
                                WHERE
                                CONCAT('"', REPLACE(c.id_level, ',', '","'), '"') LIKE '%"64"%'
                                AND c.id_study = '2'
                                AND c.course_date 
                                AND c.id_study = '2'AND c.course_date 
                                     BETWEEN "2016-10-02 00:00:00" AND "2016-10-09 23:59:59" ORDER BY c.course_date ASC
                                    ) aa
                LEFT JOIN      (
                                        SELECT     id_student, id_course
                                        FROM     tbl_course_student 
                                            WHERE id_student = '305'
                                              AND id_study = '2'
                                    ) bb ON aa.id_course = bb.id_course
                LEFT JOIN        (
                                    SELECT SUM(student_count_max) AS max_count, course_date
                                    FROM tbl_course
                                    WHERE id_study = '2'
                                         AND course_date BETWEEN "2016-10-02 00:00:00" AND "2016-10-09 23:59:59" 
                                              AND id_study = '2') bb ON bb.id_course = aa.id_course 
                LEFT JOIN        (
                                    SELECT SUM(student_count_max) AS max_count, course_date
                                    FROM tbl_course
                                    WHERE id_study = '2'AND course_date BETWEEN "2016-10-02 00:00:00" AND "2016-10-09 23:59:59" 
                                         AND CONCAT('"', REPLACE(id_level, ',', '","'), '"') LIKE '%"64"%'
                                    GROUP BY course_date, id_study
                                ) cc ON aa.course_date = cc.course_date
                LEFT JOIN       (
                                        SELECT SUM(student_count) AS student_count, course_date
                                  FROM      tbl_course 
                                    WHERE id_study = '2'
                                         AND course_date BETWEEN "2016-10-02 00:00:00" AND "2016-10-09 23:59:59" 
                                         AND CONCAT('"', REPLACE(id_level, ',', '","'), '"') LIKE '%"64"%'
                                         AND course_date BETWEEN "2016-10-02 00:00:00" AND "2016-10-09 23:59:59" AND CONCAT('"', REPLACE(id_level, ',', '","'), '"') LIKE '%"64"%'
                                    GROUP BY course_date, id_study
                                     ) dd ON aa.course_date = dd.course_date
        ) cl
        GROUP BY course_date, course_status

我的问题:我哪里做错了?我的意思是,我一遍又一遍地阅读这些查询,但我什么也没找到。

【问题讨论】:

【参考方案1】:

你有 2 倍的别名 'bb'

在您第一次加入时:

) bb ON aa.id_course = bb.id_course

在您的第二次加入中:

AND id_study = '2') bb ON bb.id_course = aa.id_course 

您需要将其中的 1 个重命名为另一个别名(并将其正确映射到您的用法中)。

【讨论】:

非常感谢,我仍然需要修复它以使其正常工作【参考方案2】:

您正在为两个内部查询使用 bb 别名。你不能那样做。每个别名都必须是唯一的。

【讨论】:

以上是关于发生数据库错误错误号:1066 不是唯一的表/别名:[重复]的主要内容,如果未能解决你的问题,请参考以下文章

错误代码:1066。不是唯一的表/别名:'circle_call_prefixes'

错误代码:1066。不是唯一的表/别名:'zones',使用外键连接 3 个数据库表 [重复]

为啥错误 #1066 - 不是唯一的表/别名:'cat_rapoarte'

错误代码:1066。不是唯一的表/别名:'ordertbl' - 无法执行内部连接 ​​MYSQL

#1066 - 错误不是唯一的表别名

Laravel - 语法错误或访问冲突:1066 不是唯一的表/别名