SQL – How to retrieve entries before and after an ID

It is possible, for example in the context of a competition that we are looking to find out which are the 5 best scores previous this one I just made. To do it in SQL, although a little tricky is not too complicated.

Publicités

The 5 before me

Select
FROM scores
WHERE score from api_scores where idscore - 34)
ORDER by score
LIMIT 5

5 after me

Select
FROM scores
WHERE score< (SELECT score from api_scores where idscore = 34) (select="" score="" from="" api_scores="" where="" idscore=""></ (SELECT score from api_scores where idscore = 34)>
ORDER by score DESC
LIMIT 5
Publicités

Leave a Reply