I don't know if the title is ok for this question, but my problem is simple.
I have a list of results, and for simplicity, it contains only idand price:
+----+-------+
| id | price |
+----+-------+
| 11 | 10 |
| 52 | 17 |
| 23 | 45 |
| 24 | 50 |
| 55 | 60 |
| 96 | 70 |
| 7 | 75 |
| 78 | 80 |
| 99 | 100 |
+----+-------+
For a given id / price, I need to find the first 2 records with a lower price and the next 2 with a higher price.
For example, for id = 55 and price = 60, the results are as follows:
+----+-------+
| id | price |
+----+-------+
| 23 | 45 |
| 24 | 50 |
| 96 | 70 |
| 7 | 75 |
+----+-------+
In a rough implementation, this can be obtained, of course, using UNION, for example:
SELECT id, price
FROM MyTable
WHERE price <= 60 AND id != 55
ORDER BY price DESC
LIMIT 0,2
UNION ALL
SELECT id, price
FROM MyTable
WHERE price >= 60 AND id != 55
ORDER BY price ASC
LIMIT 0,2
But given the fact that MyTable is actually a view obtained using a complex query, is there any other way to achieve this?
I even thought that instead of executing the query twice (with UNION) to get all the results in a single query, then use PHP to find 4 results.