查詢每組資料中最新月份的記錄

2021-06-12 08:00:54 字數 666 閱讀 9095

id

year

month

1112012

1111

2012

2111

2013

1112

2012

1112

2012

2112

2012

3112

20124

注:每一組(id,year,month) 都是唯一的,我現在要 查詢出 

(111,2013,1)

(112,2012,4)

select a.*

from a a,

(select a.id, max(b.year) as year

, max(a.month) as month 

from a a,

(select c.id

, max(c.year) year 

from a c

group by c.payitemid

) bwhere a.id=b.id and a.year=b.year

group by a.id

) bwhere a.id = b.id and a.year=b.year and a.month = b.month

用了3層巢狀才查出來,不知有沒有簡便的方法,歡迎各位大神指正

查詢組資料中最新記錄的集合

例項 現有一張統計表,是以task id和start time作為聯合主鍵的,每乙個任務可以啟動多次,這樣一來同乙個task id就會對應多個start time即多條統計記錄,現在要求將所有的任務統計出來,也就是查詢出task id唯一的集合,每條任務對應的是最新的一條統計記錄 1 select ...

mysql 分組查詢每組的最新一條資料

1.原始資料 學生成績表 2.想要獲取每個考生最新的考試成績,網上的例子 select a.from select from scoreinfo order by scoreinfo.createtime desc as a group by a.snum order by a.createtime...

mysql取出每個分組中最新的記錄

mysql取出每個分組中最新的記錄 mysql的gruop by分組功能沒有排序功能,所以我們如果想取出某個分組下的最新記錄是不太容易的,下面介紹兩種方法,一種是通過子查詢,一種是通過group concat函式來實現。一 表結構及資料插入 表的結構 test3 create table if no...