I have this relation:
╔═══════════════════╗
║ i++ name score ║
╠═══════════════════╣
║ 1 Will 123 ║
║ 2 Joe 100 ║
║ 3 Bill 99 ║
║ 4 Max 89 ║
║ 5 Jan 43 ║
║ 6 Susi 42 ║
║ 7 Chris 11 ║
║ 8 Noa 9 ║
║ 9 Sisi 4 ║
╚═══════════════════╝
with this sql:
set @username = ?;
set @uuid = ?;
SELECT * FROM (
-- Get those who scored worse (or tied)
( SELECT s.*
FROM quiz.score s
CROSS JOIN (SELECT points FROM quiz.score WHERE username = @username) and uuid=@uuid ref
WHERE s.points <= ref.points AND username <> @username
ORDER BY s.points DESC
LIMIT 2)
UNION
-- Get our reference point record
(SELECT s.* FROM quiz.score s WHERE username = @username and uuid=@uuid)
UNION
-- Get those who scored better
( SELECT s.*
FROM quiz.score s
CROSS JOIN (SELECT points FROM quiz.score WHERE username = @username and uuid=@uuid) ref
WHERE s.points > ref.points AND username <> @username
ORDER BY s.points ASC
LIMIT 2)
) slice
ORDER BY points ASC;
I got this result
╔═══════════════════╗
║ id++ name score ║
╠═══════════════════╣
║ 1 Bill 99 ║
║ 2 Max 89 ║
║ 3 Jan 43 ║
║ 4 Susi 42 ║
║ 5 Chris 11 ║
╚═══════════════════╝
but I need that result:
╔═══════════════════╗
║ id++ name score ║
╠═══════════════════╣
║ 3 Bill 99 ║
║ 4 Max 89 ║
║ 5 Jan 43 ║
║ 6 Susi 42 ║
║ 7 Chris 11 ║
╚═══════════════════╝
I need an iterator column thats count the absolute position of the founded rows in the whole table. Do you know how I can handle this?