I have a many-to-many relationship in MySQL with a users
table and posts
table linked through a third post_user
table.
The posts are made at specific locations and a lat/long value is recorded alongside each post.
I want to retrieve all users and for each user their post that is closest to a given point (in the example below, co-ordinates of 43.429124/-0.294401). I only want a single post per user (they will have lots).
I've looked at similar questions/answers and tried various queries but haven't yet been able to come up with one that works quite right.
My current best effort is this..
SELECT *, (acos(cos(radians(43.429124)) * cos(radians(posts.lat)) * cos(radians(posts.long) - radians(-0.294401)) + sin(radians(43.429124)) * sin(radians(posts.lat)))) AS xdistance
FROM users
INNER JOIN post_user ON users.id = post_user.user_id
INNER JOIN posts ON posts.id = post_user.post_id
INNER JOIN (
SELECT id, MIN(acos(cos(radians(43.429124)) * cos(radians(posts.lat)) * cos(radians(posts.long) - radians(-0.294401)) + sin(radians(43.429124)) * sin(radians(posts.lat)))) AS distance
FROM posts
GROUP BY posts.id
) subposts on posts.id = post_user.post_id AND xdistance = subposts.distance