按我的时间排名

时间:2014-01-14 18:37:34

标签: php mysql

如何在我的时间里将我当前的位置/等级作为MySQL的输出?

喜欢

SELECT rank (get rank from my time: `time`) FROM entries WHERE member = 'my userid' and quiz_id = '2'

我的表名是“条目”,分数是“时间”(最低分是最佳分数,因为它是时间。而不是最高分)

我的列是id,time,member,quiz_id

我尝试在排名上搜索Stackoverflow,但无法获得解决方案,希望有些东西可以帮助我,谢谢!

1 个答案:

答案 0 :(得分:0)

这仅计算您的测验时间的等级:

SELECT count(*)+1 as rank FROM entries 
    WHERE member = 'my userid' and quiz_id = '2' and time < :time;

或者你在桌上的最佳时间与其他时间相比:

SELECT count(*) as rank from entries
    WHERE quiz_id = '2'and 
        time <= (SELECT time FROM entries 
             WHERE member = 'my userid' and quiz_id = '2');

或者,如果您为同一个(quiz_id,member)对存储多次:

SELECT count(*) as rank from 
    (SELECT min(time) as min_time, member, quiz_id 
        FROM entries 
        WHERE time <= (SELECT min(time) FROM entries 
             WHERE member = :user_id and quiz_id = :quiz_id)
                 AND quiz_id=:quiz_id
        GROUP BY quiz_id, member) Q;

如果每个测验每个用户只有一次时间,则选择将时间与时间作为参数进行比较...

SELECT count(*)+1 as rank FROM entries 
    WHERE quiz_id = '2' and time < :time;

并且当为同一成员和相同的测验存储多次时:

SELECT count(*) as rank from 
    (SELECT DISTINCT member
        FROM entries 
        WHERE time <= :time AND quiz_id = :quiz_id) Q;