关于排序

myhuochai 2008-06-05 04:29:16
假设一个表有一个成绩项,我想从其中找出排名第10位的一个记录。其中排名是这个定的:1 1 3 3 5 6 7, 也就是并列名次的也会导致后面的人名次下降。有什么好的方法呢?直接 oder by point limit 10 得到的结果显然不对。
...全文
141 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
ccssddnnhelp 2008-06-07
  • 打赏
  • 举报
回复

用SQL可以实现。给你个思路,通过SQL语句,利用JOIN, a.point >b.point, count(*)等实现下面的查询结果

+----+-------+----------+-----+----+ 
| id | point | fullname | s1 | s2 |
+----+-------+----------+-----+----+
| 2 | 92 | user3 | 1 | 2 |
| 3 | 92 | user2 | 1 | 2 |
| 1 | 90 | user1 | 3 | |
| 4 | 85 | user4 | 4 | |
+----+-------+----------+-----+----+


这个方法以前的贴子中介绍了很多次了。一句SQL,然后你加个where s1=2 or s2=2即可.
==== ====

.
贴子分数<20:对自已的问题不予重视。
贴子大量未结:对别人的回答不予尊重。
.
海诗美妆 2008-06-06
  • 打赏
  • 举报
回复
select * from student group by 成绩 order by 成绩 limit 1, 10
myhuochai 2008-06-06
  • 打赏
  • 举报
回复
"就是这里的 user1和user2了。" => 就是这里的 user3和user2了。
myhuochai 2008-06-06
  • 打赏
  • 举报
回复
没贴上。。。

表:

create table student
(id int not null auto_increment,
point int not null default 0,
fullname varchar(16) not null,

primary key(id));


数据;

mysql> select * from student order by point desc;
+----+-------+----------+
| id | point | fullname |
+----+-------+----------+
| 2 | 92 | user3 |
| 3 | 92 | user2 |
| 1 | 90 | user1 |
| 4 | 85 | user4 |
+----+-------+----------+
4 rows in set (0.00 sec)

mysql>


myhuochai 2008-06-06
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ccssddnnhelp 的回复:]
用SQL可以实现,不难,但你至少要给出一些数据样例,
用什么字段来排序?是否有主键?.
[/Quote]

作为一个例子,表:


create table student
(id int not null auto_increment,
point int not null default 0,
fullname varchar(16) not null,

primary key(id));


数据:


mysql> desc student;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| point | int(11) | NO | | 0 | |
| fullname | varchar(16) | NO | | | |
+----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> select * from student order by point desc;
+----+-------+----------+
| id | point | fullname |
+----+-------+----------+
| 2 | 92 | user3 |
| 3 | 92 | user2 |
| 1 | 90 | user1 |
| 4 | 85 | user4 |
+----+-------+----------+
4 rows in set (0.00 sec)

mysql>


在这里面,user3, user2 排名第一,user1 排名第三,现在我要查第二名是谁,一查,没有第2名的。那就把第一名返回,就是这里的 user1和user2了。
fcoolx 2008-06-06
  • 打赏
  • 举报
回复
select * from student where point = (select point from student order by point desc limit 1,1)


这个显示的是第2,但是没有第二,所以显示2个第一
fcoolx 2008-06-06
  • 打赏
  • 举报
回复
上面显示的是第3名
fcoolx 2008-06-06
  • 打赏
  • 举报
回复
select * from student where point = (select point from student order by point desc limit 2,1)
长安宁 2008-06-06
  • 打赏
  • 举报
回复
group by point ,order by point limit 10
wildlily980 2008-06-05
  • 打赏
  • 举报
回复
oder by point limit 10,1
ccssddnnhelp 2008-06-05
  • 打赏
  • 举报
回复

用SQL可以实现,不难,但你至少要给出一些数据样例,
用什么字段来排序?是否有主键?.
==== ====

.
贴子分数<20:对自已的问题不予重视。
贴子大量未结:对别人的回答不予尊重。
.
myhuochai 2008-06-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 fcoolx 的回复:]
如果有两个9
那就没有10了,你取什么?
建议楼主写个例子,便于大家理解你的需求
[/Quote]

啊!!确实有你说的这个问题。

如果这样处理,比如对于 1 1 3 3 5 6 7, 我查第2名,查不到,那么返回它前面紧挨着的所有第1名。这个又该如何处理呢?
fcoolx 2008-06-05
  • 打赏
  • 举报
回复
如果有两个9
那就没有10了,你取什么?
建议楼主写个例子,便于大家理解你的需求
WWWWA 2008-06-05
  • 打赏
  • 举报
回复
将记录及正确结果贴出来看看
myhuochai 2008-06-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 fcoolx 的回复:]
其中排名是这个定的:1 1 3 3 5 6 7,

你既然有这个,直接 找10不就完了
[/Quote]

不是,我是用来说明排名的计算方法的,表里没这个 rank 项。
myhuochai 2008-06-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 WWWWA 的回复:]
有排名字段吗?不能直接取出来?
[/Quote]

没有…
fcoolx 2008-06-05
  • 打赏
  • 举报
回复
其中排名是这个定的:1 1 3 3 5 6 7,

你既然有这个,直接 找10不就完了
WWWWA 2008-06-05
  • 打赏
  • 举报
回复
有排名字段吗?不能直接取出来?

56,679

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧