MySql按字段分组取最大值记录 [此博文包含图片]

要求:获得按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)

时间: 2024-08-26 16:40:54

MySql按字段分组取最大值记录 [此博文包含图片]的相关文章

MySql按字段分组取最大值记录

要求:获得按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 t

SQL按字段分组取最大(小)值记录(重复记录)

SQL Server 按某一字段分组 取 最大 (小)值所在行的数据 -- 按某一字段分组 取 最大 (小)值所在行的数据 -- (爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 2007-10-23于浙江杭州) /* 数据如下: name val memo a    2   a2(a的第二个值) a    1   a1--a的第一个值 a    3   a3:a的第三个值 b    1   b1--b的第一个值 b    3   b3:b的第三个值 b    2   b2b2b2b2 b   

javascript 从对象数组中 按字段/属性取最大值或最小值

var array=[ { "index_id": 119, "area_id": "18335623", "name": "满意度", "value": "100" }, { "index_id": 119, "area_id": "18335624", "name": "满意

按某一字段分组取最大(小)值所在行的数据 分拆列值(转) 日期的推算

数据如下:name val memoa 2 a2(a的第二个值)a 1 a1--a的第一个值a 3 a3:a的第三个值b 1 b1--b的第一个值b 3 b3:b的第三个值b 2 b2b2b2b2b 4 b4b4b 5 b5b5b5b5b5*/--创建表并插入数据:create table tb(name varchar(10),val int,memo varchar(20))insert into tb values('a', 2, 'a2(a的第二个值)')insert into tb v

【转】[sql server] 分组取最大值最小值常用sql

转自:http://blog.csdn.net/xys_777/article/details/5711128 --分组取最大最小常用sql--测试环境if OBJECT_ID('tb') is not null drop table tb;gocreate table tb( col1 int, col2 int, Fcount int)insert into tbselect 11,20,1 union allselect 11,22,1 union allselect 11,23,2 un

SQL(oracle) 取得分组后最大值记录

select * from (select t.*, row_number() over(partition by 分组字段 order by 排序字段 desc ) rnfrom tablename t )where rn=1 row_number() OVER (PARTITION BY COL1 ORDER BY COL2) 表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的). 与rownum的区别在于:使用rownum进

mysql按字段分组并获取每个分组按照某个字段排序的前三条

这是原始数据 想按照brand_id分组 并获取每个分组total_num最高的前3位 SQL语句为: select a.* from data a where 3 > (select count(*) from data where brand_id = a.brand_id and total_num > a.total_num ) order by a.brand_id, a.total_num desc; 得到结果

sql分组取最大值

SELECT aa.* FROM a_user aa WHERE version = ( SELECT max(version) FROM a_user WHERE type = aa.type ) ORDER BY aa.type desc

SQL分组取每组前一(或几)条记录(排名)

mysql分组取每组前几条记录(排名) 附group by与order by的研究 http://www.jb51.net/article/31590.htm --按某一字段分组取最大(小)值所在行的数据 代码如下: /* 数据如下: name val memo a 2 a2(a的第二个值) a 1 a1--a的第一个值 a 3 a3:a的第三个值 b 1 b1--b的第一个值 b 3 b3:b的第三个值 b 2 b2b2b2b2 b 4 b4b4 b 5 b5b5b5b5b5 */ --创建表