我有一个MySQL表,我想从中提取出勤信息(学生ID,课程/科目,出勤率,日期范围,学生是否在场)。我写了以下查询:
{123456789=[{1}, {2}, {3}, {4}], 987654321=[{5}, {6}]}
这给了我正确的结果,但是当我改变学生时,它没有给我正确计算当前记录的天数。没有提到我还没有将课程参数添加到查询
中我需要查询的帮助才能返回所需的结果(计算每个学生的准确天数,以及将课程参数添加到查询中,以便查询查找特定课程的出勤记录,特定学生,指定日期范围)。
答案 0 :(得分:0)
看起来您想要分开查询:
Select (select count(*) from <database>.attendance where state = 'present' AND (dater between '$a' and '$b') AND name=(SELECT id FROM users WHERE NAME = '$stud')) as present, (select count(*) from <database>.attendance where state = 'absent' AND (dater between '$a' and '$b') AND name=(SELECT id FROM users WHERE NAME = '$stud')) as absent from <database>.attendance WHERE stud_id =(SELECT id FROM users WHERE NAME = '$stud');
试试这个:)
答案 1 :(得分:0)
使用JOIN解决它,如下所示:
SELECT u.id, a.stud_id, a.course_id, count(*) FROM attendance a
JOIN users u ON u.id=a.stud_id
JOIN courses c ON c.c_id=a.course_id
WHERE a.state='present' and dater between '2017-09-01' and '2017-09-14'
GROUP BY a.stud_id, a.course_id;
感谢您的帮助。