我正在开发学生出勤系统并尝试执行此查询:
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
但这总是显示错误“无效使用组功能”
我找不到任何错误。 请帮助我。
最佳答案
在分组依据中,您必须放置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
关于Mysql 查询不工作并显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53930845/