我想在我的课程管理系统中创建讲师的月度出勤报告。在本报告中,我需要使用嵌套的选择查询来计算讲师的存在和缺席次数,并在特定记录中显示它们。 我认为可行的方法是,如果讲师代码与主查询中正在处理的讲师代码相同,则要对每个出勤状态使用select语句!
select lecturers.*, sum(distinct courses.credit) as 'Due Credits',(select count(*) from attendances where status='1' ) as Present, (select count(*) from attendances where status='0' ) as Absent from lecturers,courses,attendances
where attendances.lecturer_code=lecturers.code
AND courses.lecturer_code=lecturers.code
group by lecturer_code
[代码,名称,部门,官方职位,学位是固定属性]
我需要知道是否有更好的方法来做到这一点!
非常感谢你!
答案 0 :(得分:0)
SELECT lecturers.*,
SUM(DISTINCT courses.credit) AS `Due Credits`,
SUM(attendances.status = 1) AS `Present`,
SUM(attendances.status = 0) AS `Absent`,
FROM lecturers
JOIN courses ON courses.lecturer_code = lecturers.code
JOIN attendances ON attendances.lecturer_code = lecturers.code
GROUP BY lecturer_code