mysql分组取每组前几条记录(排名)
2015-12-22 14:23
603 查看
资料来源:http://www.cnblogs.com/JulyZhang/archive/2011/02/12/1952213.html
按name分组取val最大的值所在行的数据。
按name分组取val最大的值所在行的数据。
--方法1: select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name --方法2: select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val) --方法3: select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name --方法4: select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name --方法5 select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
相关文章推荐
- MySQL中优化sql语句查询常用的30种方法
- MySQL用户权限管理
- MYSQL SELECT 过程 转
- MYSQL SELECT 过程 转
- 使用mysql库连接mysql
- ucenter info:can not connect to MySQL server解决办法
- MySQL5.7更改密码时出现ERROR 1054 (42S22): Unknown column 'password' in 'field list'
- 解决MySQL显示中文的问题
- Async.js解决Node.js操作MySQL的回调大坑
- 编译安装MySQL
- MySQLDump在使用之前一定要想到的事情 [转载]
- MySQL错误:2003-Can't connect to MySQL server on 'localhost' (10061)
- mysql修改字符编码
- mysql自定义循环函数
- mysql数据表中文乱码解决方案
- MySQL数据库加密与解密
- 在Window上安装Mysql
- Mysql数据库int(1)和tinyint(1)的区别&&扩展阅读
- Mysql经常使用命令
- mysql 分组取最新的一条记录(整条记录)