mysql 去重分组后取每组时间最新的一条记录
第一步:先分组后取到分组的那个id以及每个组最新的数据
-- 先根据课程分组,然后在取这组统计数据中最先的日期
select CourseID,MAX(CountDate) as CountDate
from Rep_school_class_course_count_data_code
where ClassID = '062cd8a56cbb4cd2a39d1079e44eb540'
GROUP BY CourseID
然后根据第一步查询出来的数据在去使用子查询取一次就行了(不需要全部字段可使用上边的,缺的直接补即可)
select * from Rep_school_class_course_count_data_code
where ClassID = '062cd8a56cbb4cd2a39d1079e44eb540'
and (CourseID,CountDate) in (select CourseID,MAX(CountDate) as CountDate from Rep_school_class_course_count_data_code where ClassID = '062cd8a56cbb4cd2a39d1079e44eb540'
GROUP BY CourseID)
(经常使用,作为记录提醒)
标签:code,CourseID,Rep,每组,CountDate,分组,mysql,后取 From: https://www.cnblogs.com/dongfangzhaoyue/p/17385667.html