细节
我结合了以下表格
测试结果
--------------------------------------------------------------------
| index | uid| start | stop | score|
--------------------------------------------------------------------
| 1 | 23 | 2012-06-06 07:30:20 | 2012-06-06 07:30:34 | 100 |
--------------------------------------------------------------------
| 2 | 34 | 2012-06-06 07:30:21 | 2012-06-06 07:30:40 | 100 |
--------------------------------------------------------------------
用户表
------------------------------
| id | username |
------------------------------
| 23 | MacGyver’s mum |
------------------------------
| 34 | Gribblet |
------------------------------
使用这个 sql
SELECT a.username, b.duration, b.score
FROM usertable AS a
JOIN (SELECT `uid`, `score`,
TIMESTAMPDIFF( SECOND, start, stop ) AS `duration`
FROM `testresults`
WHERE `start` >= DATE(NOW())
ORDER BY `score` DESC, `duration` ASC
LIMIT 100) AS b
ON a.id = b.uid
问题是I want to rank the results
。我认为在 sql 中执行此操作可能比在 php 中更容易/更快,因此基于http://code.openark.org/blog/mysql/sql-ranking-without-self-join这就是我尝试过的
SELECT a.username, b.duration, b.score, COUNT(DISTINCT b.duration, b.score) AS rank
FROM usertable AS a
JOIN (SELECT `uid`, `score`,
TIMESTAMPDIFF( SECOND, start, stop ) AS `duration`
FROM `testresults`
WHERE `start` >= DATE(NOW())
ORDER BY `score` DESC, `duration` ASC
LIMIT 100) AS b
ON a.id = b.uid
但我没有回到预期的排名。它只返回一行。
问题
我究竟做错了什么?只有在持续时间和分数唯一的情况下,如何才能提高排名?
更新1
使用 bdenham 的“慢速方法”对我有用,但第二种方法没有。我真的不明白“快速方法”中发生了什么。我已经发布了我正在使用的数据和结果表。你会看到排名被搞砸了。
-------------------------------------------------------------------
| index | uid| start | stop | score|
--------------------------------------------------------------------
| 1 | 32 | 2012-08-27 05:47:18 | 2012-08-27 05:47:36 | 100 | 18s
| 2 | 32 | 2012-08-27 05:50:36 | 2012-08-27 05:50:42 | 0 | 6s
| 3 | 32 | 2012-08-27 05:51:18 | 2012-08-27 05:51:25 | 100 | 7s
| 4 | 32 | 2012-08-27 05:51:30 | 2012-08-27 05:51:35 | 0 | 5s
| 5 | 32 | 2012-08-27 05:51:39 | 2012-08-27 05:51:44 | 50 | 5s
--------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| username | score | duration | @prevScore:=@currScore | @prevDuration:=@currDuration | @currScore:=r.score | @currDuration:=timestampdiff(second,r.start,r.stop) |rank |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| bob | 100 | 7 | [BLOB - 1B] | [BLOB - 1B] | 100 | 7 | 3 |
| bob | 100 | 18 | [BLOB - 0B] | [BLOB - 0B] | 100 | 18 | 1 |
| bob | 50 | 5 | [BLOB - 1B] | [BLOB - 1B] | 50 | 5 | 5 |
| bob | 0 | 5 | [BLOB - 3B] | [BLOB - 1B] | 0 | 5 | 4 |
| bob | 0 | 6 | [BLOB - 3B] | [BLOB - 2B] | 0 | 6 | 2 |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------