MYSQL 分組查詢最新的資料

2021-10-25 20:17:36 字數 534 閱讀 7929

第一種前提是資料表設計id自增

select

*from t_user where id in

(select

max(id)

from t_user group

by user_id)

第二種對子查詢進行limit限制,適用於明確資料量不會超過多少的場景,否則查不到全部資料
select

*from

(select

*from t_user order

by create_time desc

limit

10000

) a group

by user_id

這種是錯誤的

select

*from

(select

*from t_user order

by create_time desc

) a group

by user_id

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 ...

mysql查詢group by分組最新記錄資料

先group by得到分組每乙個的max id 即是最新記錄,之後通過inner join關聯得到最新記錄表。執行效率 0.031 sec select b.from table name b inner join select max id as id from table name group ...

JPA查詢分組裡的最新資料

如題,用到分組查詢,並且要求是每組的最新一條資料,見 如下 query select d from realtimedata d where d.id in select max id from realtimedata group by simcard and d.simcard in selec...