1
I am facing problems to mount a query of an address table with latitude and longitude.
I have a function that does a distance calculation, I called it:
distance_latlng_km(originLat, originLng, destLat, destLng)
.
In the table there are some important fields that I would like to return. They are: id, user_id, lat, lng
My query is like this:
SELECT id, user_id, lat, lng, distance_latlng_km(x, y, lat, lng) as distance FROM addresses;
This returns me correct, listing for me the ID of address, user owner of the address, latitude, longitude and the distance in km resulting from the calculation of latitude and longitude.
The big problem is that a user may have more than one address, then my need is to bring one address per user, if a user has more than one address, would like to shorter distance.
My current query:
SELECT b.id, b.user_id, b.lat, b.lng, distance_latlng_km(x, y, b.lat, b.lng) as distance
FROM (SELECT user_id, min(distance_latlng_km(x, y, lat, lng)) FROM address GROUP BY user_id) a
INNER JOIN address b ON b.user_id = a.user_id;
That worked yes, now my problem is another, in which I need to put it together and use in conjunction with four more tables, can you help me? If there was a channel where we could talk, it would be nice.
– Italo Izaac
Mark the answer as correct and edit the question indicating the new data and edit the answer if you can help.
– Paulo R. F. Amorim
@Italoizaac if the question has been answered, ideally you mark the answer as correct and create another question with the new problem.
– rLinhares