mysql查询不工作并显示错误

vq8itlhq  于 2021-06-17  发布在  Mysql
关注(0)|答案(3)|浏览(351)

我正在开发学生出勤系统并尝试执行此查询:

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

但这表明一个错误总是“组函数的无效使用”
我找不出什么毛病。请帮帮我。

x6492ojm

x6492ojm1#

在group by中,必须放置所有非聚合列:

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
watbbzwu

watbbzwu2#

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
vuv7lop3

vuv7lop33#

我建议这样写:

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' 被认为迟到?

相关问题