如題,用到分組查詢,並且要求是每組的最新一條資料,見**如下:
@query
("select d from realtimedata d "
+"where d.id in (select max(id) from realtimedata group by simcard) "
+" and d.simcard in (select cd.simcard from device cd where cd.customer.id=?1)"
)public page
finddatalistbycustomerid
(long customerid,pageable pageable)
;
解釋:
device表時儲存使用者的裝置資訊,其中字段 customer customer為使用者。
simcard欄位存放裝置的唯一識別碼。
select max(id) from realtimedata group by simcard) 查出按裝置識別碼的最新資訊
(select cd.simcard from device cd where cd.customer.id=?1) 查出使用者名稱下的裝置的識別碼
realtimedata 和 device都是實體類
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 分組查詢最新的資料
第一種前提是資料表設計id自增select from t user where id in select max id from t user group by user id 第二種對子查詢進行limit限制,適用於明確資料量不會超過多少的場景,否則查不到全部資料select from selec...
mysql 查詢分組裡的第n條資料,前n條資料
select uname,salary,address,new rank from select uname,salary,address,如果臨時變數等於 address,就 1,否則從1開始 if tmpaddress address,rank rank 1 rank 1 as new rank...