i have a table with database and i want get two data before current id and get two data after current id.
primary_key id
1 345
2 356
3 400
4 102
5 210
6 190
Case:
- If current id defined 400, the results as before is 356 & 345 and after is 102 & 210
- If current id defined 210, the results as before is 102 & 400 and after is 190
- If current id defined 356, the results as before is 345 and after is 400 & 102
- If current id defined 345, the results as before is NULL and after is 356 & 400
- If current id defined 190, the results as before is 210 & 102 and after is NULL
I try this SQL but not working fine,
$define_id = 400;
SELECT *
FROM table_name
WHERE (
id = IFNULL(
(
SELECT MIN(id)
FROM table_name
WHERE id > $define_id
), 0 )
OR id = IFNULL(
(
SELECT MAX(id)
FROM table_name
WHERE id < $define_id
), 0 )
)
LIMIT 2
The code success to get before and after data, but only one before and one after. I want get result two before and two after.
Please help.