How can i get my current position / rank as output in MySQL for my time?
Like
SELECT rank (get rank from my time: `time`) FROM entries WHERE member = 'my userid' and quiz_id = '2'
My table name is "entries" and the score is "time" (which the lowest is the best score, because it's time. and not highest score)
My columns is id, time, member, quiz_id
I tried searching on Stackoverflow on ranks, but couldn't get a solution, hope something can help me though, thanks!
This counts your rank for your quiz times only:
SELECT count(*)+1 as rank FROM entries
WHERE member = 'my userid' and quiz_id = '2' and time < :time;
Or your best time in the table compared to other's 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');
Or if you have multiple times stored for the same (quiz_id,member) pair:
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 rank compared to time as parameter, if there is only one time per user per quiz...
SELECT count(*)+1 as rank FROM entries
WHERE quiz_id = '2' and time < :time;
and when multiple times are stored for the same member and same quiz:
SELECT count(*) as rank from
(SELECT DISTINCT member
FROM entries
WHERE time <= :time AND quiz_id = :quiz_id) Q;