MacBook avec code sur un bureau

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 what are the 5 best scores preceding the one I just made. To do it in SQL, although a little tricky, it is not too complicated.

The 5 before me

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

the 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

Leave a comment