I have a separate table in MYSQL with the addresses of the users containing the location information (latitude and longitude), each user being able to have more than one registered address. My goal is that when performing a search for a location, all the users closest to that location are listed.
The problem is that I can not do DISTINCT
so that each user is listed only once in SELECT
. I searched deeper and saw that GROUP BY
could solve the problem. It does indeed group users by preventing repetition, however it does not maintain the correct order that would be the shortest distance.
This case can be easily seen in the Maps documentation but DISTINCT is not done. link
Result without GROUP BY
(This is what I need, but without repeating the users):
ID_PLACE | ID_USER | NAME_USER | DISTANCE
2 1 MARIA 5
3 2 KEVIN 6
1 1 MARIA 8
4 2 KEVIN 10
Result with GROUP BY
:
ID_PLACE | ID_USER | NAME_USER | DISTANCE
1 1 MARIA 8
3 2 KEVIN 6
When in fact it would have to be this way ...
ID_PLACE | ID_USER | NAME_USER | DISTANCE
2 1 MARIA 5
3 2 KEVIN 6
With GROUP BY
, distance ordering seems to be being ignored and is apparently being done by PLACE_ID
.
My SELECT
is this:
SELECT place_id, id_user,
(6371 * acos(
cos( radians(-30.053831) )
* cos( radians( lat ) )
* cos( radians( lng ) - radians(-51.191810) )
+ sin( radians(-30.053831) )
* sin( radians( lat ) )
)
) AS distancia
FROM enderecos
GROUP BY id_user
HAVING distancia < 25
ORDER BY distancia ASC;