mysql 去重分组后取每组时间最新的一条记录

mysql 去重分组后取每组时间最新的一条记录

第一步:先分组后取到分组的那个id以及每个组最新的数据

    -- 先根据课程分组,然后在取这组统计数据中最先的日期

    select CourseID,MAX(CountDate) as CountDate 

    from Rep_school_class_course_count_data_code

    where ClassID = '062cd8a56cbb4cd2a39d1079e44eb540' 

    GROUP BY CourseID

然后根据第一步查询出来的数据在去使用子查询取一次就行了(不需要全部字段可使用上边的,缺的直接补即可)

  1. select * from Rep_school_class_course_count_data_code
  2. where ClassID = '062cd8a56cbb4cd2a39d1079e44eb540'
  3. and (CourseID,CountDate) in (select CourseID,MAX(CountDate) as CountDate from Rep_school_class_course_count_data_code where ClassID = '062cd8a56cbb4cd2a39d1079e44eb540'
  4. GROUP BY CourseID)

(经常使用,作为记录提醒)

posted @ 2023-05-09 17:05  华了个华  阅读(97)  评论(0)    收藏  举报