嘿我正在构建非常复杂的查询,我不知道它的计算是否复杂,但由于我是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
我的问题:我哪里做错了?我的意思是,我一直在阅读那些查询,但我一无所获。
答案 0 :(得分:1)
你有两倍的别名' bb'
在你的第一次加入中:
) bb ON aa.id_course = bb.id_course
在你的第二次加入中:
AND id_study = '2') bb ON bb.id_course = aa.id_course
您需要将其中一个重命名为另一个别名(并在您的使用情况下正确映射)。
答案 1 :(得分:1)
您正在使用#1
别名进行两次内部查询。你不能这样做。每个别名都必须是唯一的。