How to get 5 records before AND after a record with a specific identifier?

I have a table with a name scoreswith columns idand score. I want to access a specific record on it id, as well as 5 records before and after it. Is there a way in SQL to say "grab a bill with id nand xelements before and after it?"

+3
source share
1 answer

Try:

  SELECT * 
    FROM scores 
   WHERE score >= n
ORDER BY score ASC
   LIMIT 6

 UNION

  SELECT * 
    FROM scores 
   WHERE score < n
ORDER BY score DESC
   LIMIT 5

The syntax may vary slightly depending on which database server you are using.

+3
source

Source: https://habr.com/ru/post/1737261/


All Articles