#1248 - 每个派生表必须有自己的别名

时间:2014-03-22 21:02:28

标签: mysql mysql-error-1064 mysql-error-1054

以下代码一直给我别名错误:

SELECT name,
       semester
FROM   (SELECT name,
               semester,
               (SELECT COUNT(*)
                FROM   instructors AS i
                       JOIN teaches
                         ON i.id = teaches.instructor_id
                       JOIN schedules
                         ON teaches.schedule_id = schedules.id
                       JOIN sections AS s
                         ON schedules.section_id = s.id
                WHERE  i.name = instructors.name
                       AND hour_start > 10
                       AND ( hour_end < 17
                              OR ( hour_end = 17
                                   AND min_end <> 0 ) )
                       AND s.semester = sections.semester)  AS c1,
               (SELECT COUNT(*)
                FROM   instructors AS i2
                       JOIN teaches
                         ON i2.id = teaches.instructor_id
                       JOIN schedules
                         ON teaches.schedule_id = schedules.id
                       JOIN sections AS s2
                         ON schedules.section_id = s2.id
                WHERE  i2.name = instructors.name
                       AND s2.semester = sections.semester) AS c2
        FROM   instructors
               JOIN teaches
                 ON instructors.id = teaches.instructor_id
               JOIN schedules
                 ON teaches.schedule_id = schedules.id
               JOIN sections
                 ON schedules.section_id = sections.id
        WHERE  c1 = c2)
GROUP  BY name,
          semester
ORDER  BY name,
          semester; 

1 个答案:

答案 0 :(得分:2)

为子查询添加别名,所有内部查询都有别名,如c1,c2,但您的第一级子查询没有别名

SELECT t.name,
       t.semester
FROM   ( ........) t
GROUP  BY t.name,
          t.semester
ORDER  BY t.name,
          t.semester;