我正在开发学生出勤系统,并尝试执行以下查询:
select s.full_name,
s.student_id,
count(a.id) as total_present,
count(CASE WHEN TIMEDIFF(min(a.punch_in_time),'10:00:00') THEN '1' END) 'late'
from student s, attendance_record a
where a.student_id=s.student_id
and a.punch_in_date BETWEEN '2018-12-26' and '2018-12-26'
group by s.student_id
但这总是显示错误“组功能的无效使用”
我找不到任何错误。 请帮助我。
答案 0 :(得分:3)
在分组依据中,您必须放置 ALL 非聚合列:
select s.full_name,
s.student_id,
count(a.id) as total_present,
count(CASE WHEN TIMEDIFF(min(a.punch_in_time),'10:00:00') THEN '1' END) 'late'
from student s, attendance_record a
where a.student_id=s.student_id
and a.punch_in_date BETWEEN '2018-12-26' and '2018-12-26'
group by s.student_id , s.full_name
注意:最好使用“ LEFT JOIN”或“ INNER JOIN”联接表,因为它更具可读性
select s.full_name,
s.student_id,
count(a.id) as total_present,
count(CASE WHEN TIMEDIFF(min(a.punch_in_time),'10:00:00') THEN '1' END) 'late'
from student s
INNER JOIN attendance_record a ON a.student_id=s.student_id
where
a.punch_in_date BETWEEN '2018-12-26' and '2018-12-26'
group by s.student_id , s.full_name
答案 1 :(得分:0)
SELECT s.full_name,
s.student_id,
Count(a.id) AS total_present,
Count(CASE
WHEN TIMEDIFF(Min(a.punch_in_time), '10:00:00') THEN '1'
END) 'late'
FROM student s,
attendance_record a
WHERE a.student_id = s.student_id
AND a.punch_in_date BETWEEN '2018-12-26' AND '2018-12-26'
GROUP BY s.full_name,s.student_id
答案 2 :(得分:0)
我建议这样写:
select s.full_name, s.student_id,
count(a.student_id) as total_present,
sum(a.punch_in_time > '10:00:00') as total_late
from student s left join
attendance_record a
on a.student_id = s.student_id and
a.punch_in_date between '2018-12-26' and '2018-12-26'
group by s.full_name, s.student_id;
您对late
的逻辑很奇怪。为什么将时间戳为'09:59:59'
的人视为迟到者?