Don't know if the title for this question is ok, but my problem is simple.
I have a list of results and for simplicity, it contains only an id and a 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 first 2 records with a lower price and next 2 with a higher price.
For example, for id = 55 and price = 60, the results would be:
+----+-------+
| id | price |
+----+-------+
| 23 |   45  |
| 24 |   50  |
| 96 |   70  |
|  7 |   75  |
+----+-------+
In a rough implementation, this can be of-course obtained with a UNION, like this:
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 behind, is there another way to achieve this?
I was even thinking that instead of running the query twice (with the UNION), to get all the results in one query and then to use PHP to find the 4 results.
 
    

 
    