What I want to achieve is to get only the closest result for every trail_part_id. However I am stuck, I tried to use GROUP BY trail_part_id, but that did not work either as I got the following error message:
SELECT list is not in GROUP BY clause and contains nonaggregated column ‘p.latpoint’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
I know I am missing something, please put me in the right direction.
SELECT DISTINCT(trail_part_id), 111.045 * DEGREES(ACOS(COS(RADIANS(latpoint)) * COS(RADIANS(latitude)) * COS(RADIANS(longpoint) - RADIANS(longitude)) + SIN(RADIANS(latpoint)) * SIN(RADIANS(latitude)))) AS distance_in_km FROM waypoints JOIN (SELECT ' . $latitude . ' AS latpoint, ' . $longitude . ' AS longpoint) AS p ORDER BY distance_in_km ASC
Advertisement
Answer
There was a few errors in my code.
- As @a_horse_with_no_name explained, DISTINCT is not a function. So I removed it.
- What I had to do was to select the lowest value of “distance_in_km” by wrapping it with MIN().
After this I did get the closest waypoint, and that was it, now I can use GROUP BY trail_part_id and get the expected result.