So you want to select a range of rows, row_number should be your friend as you say you can't rely on the id. Check this widely accepted and detailed SO answer about the use of mySQL ROW_NUMBER
.
Then try this working SQL Fiddle code. But I'm still tuning it a bit as it does recover the amount of rows you need by setting up the values on the WHERE tt.row_number between 3 and 7
line but those lines are not around a row selected by you somehow. Tricky thing.
Test data:
id col1
1 adfg
2 sg5r
3 34tdfgdf
4 ergdfd
5 ghjghghj
6 4gfhfrgfr
7 zxcxvfdf
8 sfdgd
9 s8545454
10 7jhgdfe45
11 fbvmso
12 sdfg9dj3
13 zjvjude89
14 _sdfdi3
The query:
SELECT Table1.*
FROM Table1
WHERE col1 IN (
SELECT col1
FROM (SELECT t.col1,
@curRow := @curRow + 1 AS row_number
FROM Table1 t
JOIN (SELECT @curRow := 0) r
) tt
WHERE tt.row_number between 3 and 7
)
ORDER BY col1;
Result data:
ID COL1
3 34tdfgdf
6 4gfhfrgfr
4 ergdfd
5 ghjghghj
7 zxcxvfdf