mysql 分组后取每组时间最新的一条记录 电脑版发表于:2023/3/27 11:15 **第一步:先分组后取到分组的那个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) ```