Get rows above and below (neighbouring rows) a certain row, based on two criteria SQL
The values for id 5 and LIMIT must be calculate beforehand and passed on to the query, because LIMIT must have fixed values or you have to use a prepared statement


CREATE TABLE table1
    (`id` int, `level` int, `score` int, `timestamp` varchar(19))
;
    
INSERT INTO table1
    (`id`, `level`, `score`, `timestamp`)
VALUES
    (4, 1, 70, '2021-01-14 21:50:38'),
    (3, 1, 90, '2021-01-12 15:38:0'),
    (1, 1, 20, '2021-01-14 13:10:12'),
    (5, 1, 50, '2021-01-13 12:32:11'),
    (7, 1, 50, '2021-01-14 17:15:20'),
    (8, 1, 55, '2021-01-14 09:20:00'),
    (10, 2, 99, '2021-01-15 10:50:38'),
    (2, 1, 45, '2021-01-15 10:50:38')
;




SELECT 
    `id`, `level`, `score`
FROM
    ((SELECT 
        `id`, `level`, `score`, 1 orderby
    FROM
        table1 a
    WHERE
        level = 1
            AND `score` &gt; (SELECT 
                `score`
            FROM
                table1
            WHERE
                id = 5)
    ORDER BY `score` ASC
    LIMIT 2) UNION ALL (SELECT 
        `id`, `level`, `score`, 2 orderby
    FROM
        table1 a
    WHERE
        id = 5) UNION ALL (SELECT 
        `id`, `level`, `score`, 3 orderby
    FROM
        table1 a
    WHERE
        level = 1
            AND `score` &lt;= (SELECT 
                `score`
            FROM
                table1
            WHERE
                id = 5)
            AND id &lt;&gt; 5
    ORDER BY `score` DESC
    LIMIT 3)) t1
ORDER BY orderby ASC , score DESC


id | level | score
-: | ----: | ----:
 4 |     1 |    70
 8 |     1 |    55
 5 |     1 |    50
 7 |     1 |    50
 2 |     1 |    45
 1 |     1 |    20


db&lt;&gt;fiddle here