1. 程式人生 > >MySql按字段分組取最大值記錄 [此博文包含圖片]

MySql按字段分組取最大值記錄 [此博文包含圖片]

from htm 技術 方法 sin gid 最大值 count not

技術分享

要求:獲得按table1_id分組,並且age最大的記錄信息,即2、3、5條 方法一: select * from (select * from table2 order by age desc) as a group by a.table1_id 方法二: select a.* from table2 as a where age = (select max(age) from table2 where a.table1_id=table1_id) 方法三: select a.* from table2 as a where not exists (select * from table2 where table1_id=a.table1_id and age>a.age) 方法四:
select a.* from table2 as a where exists (select count(*) from table2 where table1_id=a.table1_id and age>a.age having count(*)=0)

MySql按字段分組取最大值記錄 [此博文包含圖片]