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 */ --创建表
mysql分组取每组前几条记录(排名) 附group by与order by的研究,需要的朋友可以参考下 --按某一字段分组取最大(小)值所在行的数据 复制代码代码如下: /* 数据如下: 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 */ --创建表并插入数据: 复制代码代码如下: create
按name分组取最大的两个val: [比当前记录val大的条数]小于2条:即当前记录为为分组中的前两条 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val; val from tb where name=a.name order by val desc) order by a.name,a.val; ) order by a.name;
先初始化一些数据,表名为 test ,字段及数据为: SQL执行结果为:每个 uid 都只有 3 条记录. SQL语句为: SELECT * FROM test main WHERE (SELECT COUNT(1) FROM test sub WHERE main.uid = sub.uid AND main.gid > sub.gid ) < 3;
按 gpcode每组 取每组 f4 最大的那条记录: 方法一: select * from calcgsdataflash a where gscode = 'LS_F' and ymd >= 20171117 and ymd <= 20171117 and 1 >= (select count(*) from calcgsdataflash b where gscode = 'LS_F' and ymd >= 20171117 and ymd <= 20171117 and
select *from hotel_addition_orders awhere (select count(*) from hotel_addition_orders where hotel_order_id = a.hotel_order_id and type > a.type)<2order by a.hotel_order_id asc, a.type desc;#班级排名----比我分数高的人少于2,那我就排第二名select *from hotel_addition_order
/*第一种实现方法,效率低并且有错误*/ DECLARE @DD DATETIME SET @DD = GETDATE() SELECT a.GoodsID , a.Account , a.LastUpdate FROM dbo.tb_App_Goods a LEFT JOIN dbo.tb_App_Goods b ON a.Account = b.Account AND a.LastUpdate > b.LastUpdate GROUP BY a.GoodsID , a.Account , a
SELECT a.* FROM chat_log a INNER JOIN (SELECT MAX(id) id,to_user FROM chat_log GROUP BY to_user)b ON a.to_user=b.to_user AND a.id=b.id ORDER BY a.to_user;
SELECT A.* FROM digital_asset A, (SELECT name, max(last_updated) max_day FROM digital_asset GROUP BY name) B WHERE A.name = B.name AND A.last_updated = B.max_day SELECT A . * FROM bbs_threads A, ( SELECT digest, max( dateline ) max_dateline FROM bbs_
一.对分组的记录取前N条记录:例子:取前 2条最大(小)的记录 .用子查询: SELECT * FROM right2 a WHERE > (SELECT COUNT(*) FROM right2 b WHERE b.id=a.id AND b.account>a.account) ORDER BY a.id,a.account DESC .用exists半连接: SELECT * FROM right2 a WHERE EXISTS (SELECT COUNT(*) FROM right2
MySQL中GROUP BY分组取前N条记录实现 mysql分组,取记录 GROUP BY之后如何取每组的前两位下面我来讲述mysql中GROUP BY分组取前N条记录实现方法. 这是测试表(也不知道怎么想的,当时表名直接敲了个aa,汗~~~~): 结果: 方法一: SELECT a.id,a.SName,a.ClsNo,a.Score FROM aa a LEFT JOIN aa b ON a.ClsNo=b.ClsNo AND a.Score<b.Score group by a.id,a.