来源:自学PHP网 时间:2015-04-16 10:51 作者: 阅读:次
[导读] -- MySQL分组排序取前N条记录的最简洁的单条sql。...
-- MySQL分组排序取前N条记录的最简洁的单条sql。
N>( 结果如下: mysql> SELECT * FROM (SELECT cid,author,COUNT(*) AS number FROM test GROUP BY cid,author) a -> WHERE -> 3>( -> SELECT COUNT(*) -> FROM (SELECT cid,author,COUNT(*) AS number FROM test GROUP BY cid,author) b -> WHERE a.cid=b.cid AND a.number<b.number -> )ORDER BY cid,number DESC; +------+----------------+--------+ | cid | author | number | +------+----------------+--------+ | 1 | test3 | 4 | | 1 | test2 | 3 | | 1 | test1 | 2 | | 2 | test33 | 4 | | 2 | test22 | 3 | | 2 | test11 | 2 | | 200 | 200test_nagios | 1 | +------+----------------+--------+ 7 rows in set (0.00 sec) N就是取分组之后的最前面几个判断,N=3就是取前3个 -- 生成自动数字序列 |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com