mysql分组查询最新数据

时间:2021-09-07 09:49:22

select user_id,sbp,dbp,pulse from p_bp as a, (select max(id) as id, max(measure_at) as measure_at from p_bp as b group by user_id ) as b where a.id=b.id and a.measure_at = b.measure_at;
select id,max(measure_at), max(id) as id from p_bp as b group by user_id
select user_id,sbp,dbp,pulse from (select * from p_bp order by measure_at desc) as a group by user_id;

 mysql取分组后最新的一条记录,下面两种方法.一种是先筛选 出最大和最新的时间,在连表查询.一种是先排序,然后在次分组查询(默认第一条),就是最新的一条数据了