我在SQL中遇到问题,我正在尝试在我正在构建的注册系统中计算出一个班级的学生,但是它不会让我在子查询中使用父列,
SELECT A.student_id, TRUNCATE((100 - ((100/B.reg_num) * C.abs_num)), 0) AS attendance FROM
students A
JOIN (
SELECT COUNT(*) AS reg_num
FROM students
JOIN seminargroup_student ON seminargroup_student.student_id = students.student_id
JOIN seminar_group ON seminar_group.seminar_group_id = seminargroup_student.seminar_group_id
JOIN modules ON modules.module_id = seminar_group.seminar_group_module_id
JOIN register_seminar ON register_seminar.seminar_id = seminar_group.seminar_group_id
JOIN registers ON registers.register_id = register_seminar.register_id
WHERE modules.module_id =1 AND students.student_id = A.student_id
) B
JOIN (
SELECT COUNT(*) AS abs_num
FROM students
JOIN seminargroup_student ON seminargroup_student.student_id = students.student_id
JOIN seminar_group ON seminar_group.seminar_group_id = seminargroup_student.seminar_group_id
JOIN modules ON modules.module_id = seminar_group.seminar_group_module_id
JOIN absence ON students.student_id = absence.student_id
WHERE modules.module_id =1 AND students.student_id = A.student_id
) C
返回:#1054 - 'where子句'中的未知列'A.student_id'
感谢您的帮助!
答案 0 :(得分:1)
它不会让我使用子查询中的父列
一般来说,您不需要在FROM中的子查询中引用父项。
相反,您只需将加入字段添加到子查询中的SELECT
和GROUP BY
子句中,然后加入
e.g。
SELECT students.student_id,
Truncate(( 100 - ( ( 100 / b.reg_num ) * c.abs_num ) ), 0) AS attendance
FROM students
JOIN (SELECT a.studentid,
Count(*) AS reg_num
FROM students A
JOIN seminargroup_student
ON seminargroup_student.student_id = A.student_id
JOIN seminar_group
ON seminar_group.seminar_group_id =
seminargroup_student.seminar_group_id
JOIN modules
ON modules.module_id =
seminar_group.seminar_group_module_id
JOIN register_seminar
ON register_seminar.seminar_id =
seminar_group.seminar_group_id
JOIN registers
ON registers.register_id = register_seminar.register_id
GROUP BY a.studentid) A
ON students.studentid = a.student.id
JOIN (SELECT a.studentid,
Count(*) AS abs_num
FROM students aA
JOIN seminargroup_student
ON seminargroup_student.student_id = a.student_id
JOIN seminar_group
ON seminar_group.seminar_group_id =
seminargroup_student.seminar_group_id
JOIN modules
ON modules.module_id =
seminar_group.seminar_group_module_id
JOIN absence
ON a.student_id = absence.student_id
GROUP BY a.studentid) b
ON students.studentid = b.student.id
作为旁注,如果使用左连接并且在PK字段上执行DISTINCT COUNT而不是在*
上,则不必执行两个子查询SELECT
A.student_id,
TRUNCATE((100 - ((100/counts.reg_num) * counts.abs_num)), 0) AS attendance
FROM
students A
JOIN
(SELECT
COUNT(DISTINCT absence.absence_id) AS abs_num , --OR whatever the PK is
COUNT(DISTINCT registers.regeister_id) as reg_num,
students.student_id
FROM students
JOIN seminargroup_student
ON seminargroup_student.student_id = students.student_id
JOIN seminar_group
ON seminar_group.seminar_group_id =
seminargroup_student.seminar_group_id
JOIN modules
ON modules.module_id = seminar_group.seminar_group_module_id
LEFT JOIN register_seminar
ON register_seminar.seminar_id = seminar_group.seminar_group_id
LEFT JOIN registers
ON registers.register_id = register_seminar.register_id
LEFT JOIN absence
ON students.student_id = absence.student_id
GROUP BY
students.student_id) COUNTS
ON a.student_id = coutnts.student_ID
答案 1 :(得分:0)
尝试将查询重写为:
SELECT students.student_id, TRUNCATE((100 - ((100/B.reg_num) * C.abs_num)), 0) AS attendance FROM
students
JOIN (
SELECT COUNT(*) AS reg_num
FROM students A
JOIN seminargroup_student ON seminargroup_student.student_id = A.student_id
JOIN seminar_group ON seminar_group.seminar_group_id = seminargroup_student.seminar_group_id
JOIN modules ON modules.module_id = seminar_group.seminar_group_module_id
JOIN register_seminar ON register_seminar.seminar_id = seminar_group.seminar_group_id
JOIN registers ON registers.register_id = register_seminar.register_id
HAVING modules.module_id =1 AND students.student_id = A.student_id
) B
JOIN (
SELECT COUNT(*) AS abs_num
FROM students A
JOIN seminargroup_student ON seminargroup_student.student_id = A.student_id
JOIN seminar_group ON seminar_group.seminar_group_id = seminargroup_student.seminar_group_id
JOIN modules ON modules.module_id = seminar_group.seminar_group_module_id
JOIN absence ON A.student_id = absence.student_id
HAVING modules.module_id =1 AND students.student_id = A.student_id
) C
UPD :WHERE子句替换为HAVING,别名在子查询中移动。 由于在SELECT之前计算了WHERE,可能会导致错误。
这些问题也可以帮助您解决问题:
1。 Unknown Column In Where Clause
2。 WHERE vs HAVING