I want to be able to add custom rows to the result set. Let’s say i have the following query:
SELECT x, y, count(*) c
FROM cache
WHERE cache_id = 1
GROUP BY cache_key
An exemplary result may be:
x y c
4 5 2
12 16 6
Now i want to add related rows to the result set. Let’s say i want to add 4 rows per result which have neighbored coordinates and a lowered count to have a result like this:
x y c
4 5 2 (original row)
3 5 1 (added rows)
5 5 1
4 4 1
4 6 1
12 16 6 (original row)
11 16 3 (added rows)
13 16 3
12 15 3
12 17 3
Spoken in images, instead of this:
2
6
i want this:
1
1 2 1
1
3
3 6 3
3
Is that possible with pure MySQL? The result is processed in PHP afterwards, if there is no way to achieve this.
It may be possible to do a self join to achieve this purely in MySQL. However, I don’t think it would be worth the trouble.
My suggestion would be to loop over the original result set in PHP and add your neighboring coordinates. The final result would be what you need.