得到一条记录 按照 成绩字段排序 的名次 比如说 我要找 id=2 按照成绩排名 他是第多少名 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 自联,统计大于自己分数的人的个数+1SQL,需要你额外描述,另外,中文一般说 他是第几名 比如说 id num1 102 1003 20查询 id=2 的记录 按照num排序得到结果 1 (按照num排序 id=2 他的num是最大的 所以排名第一 返回 1) 同名如何处理?select a.id,a.num,count(b.id) from tt a inner join tt b on a.num<=b.numwhere a.id=2group by a.id,a.num mysql> select *from tb1;+------+------+| id | num |+------+------+| 1 | 10 || 2 | 100 || 3 | 20 |+------+------+3 rows in set (0.00 sec)mysql> select * from ( -> select id,num,(select count(1) from tb1 where num>=t.num) rank from tb1 t) a -> where id = 2;+------+------+------+| id | num | rank |+------+------+------+| 2 | 100 | 1 |+------+------+------+1 row in set (0.00 sec) select * from ( -> select id,num,(select count(1)+1 from tb1 where num>t.num) rank from tb1 t) a -> where id = 2; select count(*)+1 from 比如说 where num>(select num from 比如说 where id=2) Mysql视图和表关联查询速度问题 MySQL游标向后遍历 一个库里有一张表卡死了 phpmyadmin同时管理多个MYSQL服务器的方法 mysql+php 插入数据乱码,读取不乱码? myodbc3.5支持mysql5.0.24a最新版本么? MySQL中如何在cmd或shell中显示出某个数据库的ER图 count(*)和count(id)的效率问题 请问用sql语句怎么得到一张表的字段名字?急,请帮忙! mysql中导出文本问题 MYSQL如何实现有条件区分的触发器? 问一个mysql的小技巧
id num
1 10
2 100
3 20查询 id=2 的记录 按照num排序得到结果 1 (按照num排序 id=2 他的num是最大的 所以排名第一 返回 1)
select a.id,a.num,count(b.id) from tt a inner join tt b on a.num<=b.num
where a.id=2
group by a.id,a.num
+------+------+
| id | num |
+------+------+
| 1 | 10 |
| 2 | 100 |
| 3 | 20 |
+------+------+
3 rows in set (0.00 sec)
mysql> select * from (
-> select id,num,(select count(1) from tb1 where num>=t.num) rank from tb1 t) a
-> where id = 2;
+------+------+------+
| id | num | rank |
+------+------+------+
| 2 | 100 | 1 |
+------+------+------+
1 row in set (0.00 sec)
-> select id,num,(select count(1)+1 from tb1 where num>t.num) rank from tb1 t) a
-> where id = 2;