码迷,mamicode.com
首页 > 数据库 > 详细

mysql分组取前N记录

时间:2017-05-17 00:56:43      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:main   sdn   rom   ref   group by   http   ble   href   from   

http://blog.csdn.net/acmain_chm/article/details/4126306

http://bbs.csdn.net/topics/390958705

我只用到了其中的特殊形式,就是 分组取最新的一条记录:

select *
from (select * from Table1 order by Score desc) t
group by ClsNo

利用的是 group by 只取第一条记录,所以需要先把需要的记录排序到第一位

select * 
from Table1 a
where not exists (select 1 from Table1 where ClsNo=a.ClsNo and Score>a.Score);

这个是利用子查询先查出小于最高值的记录,再排除出最高记录(感觉绕了一圈)

mysql分组取前N记录

标签:main   sdn   rom   ref   group by   http   ble   href   from   

原文地址:http://www.cnblogs.com/mitang/p/6864343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!