1. 程式人生 > >mysql分組查詢最新資料

mysql分組查詢最新資料

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取分組後最新的一條記錄,下面兩種方法.一種是先篩選 出最大和最新的時間,在連表查詢.一種是先排序,然後在次分組查詢(預設第一條),就是最新的一條資料了