Let's pretend we 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 ║
╚═══════════════════╝
Now I need a subset based on the data I am searching for. For instance I'm searching for the fith place. In my result I need more than the record of Jan, I need the two records before Jan and the two records behind Jan too. So I have the following resultset:
╔═══════════════════╗
║ id++ name score ║
╠═══════════════════╣
║ 3 Bill 99 ║
║ 4 Max 89 ║
║ 5 Jan 43 ║
║ 6 Susi 42 ║
║ 7 Chris 11 ║
╚═══════════════════╝
That is the sql I got:
select @a:= id from quiz.score where username = 'Jan';
set @i=0;
SELECT @i:=@i+1 as Platz, s.*
FROM quiz.score s where id BETWEEN @a-5 AND @a+5
order by points desc;
The problem here is that @a
is the id
of the record. Is there a way to use the calculated value @i:=@i+1
?
Thx a lot for your help.