Mysql 查询不工作并显示错误

标签 mysql sql database

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

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/

相关文章:

php - 几分钟后如何销毁 session

mysql - sql select查询返回大写

sql - 如何判断 sqlite 列是否为 AUTOINCREMENT?

sql - Oracle where 子句性能不佳

database - TDengine子查询核心转储

sql - 在 postgresql 中更新具有不同值的所有重复行

php - 一个 php session 变量导致重定向时取消 session 设置

php - 如何在 php 中将评论值附加到各个帖子

php - 自动建议机场 - 如何在多个列中订购 "best match first"?

sql - 祖父外键应该存储在孙表中吗?