Go Longhorns!
Let's start by finding the right row in the location table.
SELECT DISTINCT location_id
FROM locations
ORDER BY your_spherical_cosine_law_distance_formula
LIMIT 1
That gets you the unique location id.
Now you want to use that as a subquery to get the appropriate profiles rows. That you do like this:
SELECT whatever
FROM (
SELECT DISTINCT location_id
FROM locations
ORDER BY your_spherical_cosine_law_distance_formula
LIMIT 1
) AS one
JOIN location_assignment AS la ON one.location_id = la.location_id
JOIN profiles AS p on p.profile_id =la.profile_id
That should give you the appropriate list of profiles rows, without duplications.
You didn't ask about this, but I hope you don't have too many locations rows. The query you're using will necessarily scan through the whole table and do a lot of math for each row. Your HAVING
clause really doesn't help. To make this faster you need to combine a distance search with a bounding-rectangle search. This might help. http://www.plumislandmedia.net/mysql/haversine-mysql-nearest-loc/