根据
previous similar question
这应该有效:
SELECT * FROM
(SELECT clinics.id as cid, clinics.lat, clinics.lng, clinics.opening_hours,
clinics.logo, clinics.address, clinics.city, clinics.name, clinics.description,
clinics.zip, clinics.phone_number, clinics.email, clinics.url, clinics.gmaps_link,
clinics.marker,
countries.full_name AS country,
(6378 * acos(
cos(radians(-33.84801)) * cos(radians(lat)) *
cos(radians(lng) - radians(151.06488)) +
sin(radians(-33.84801)) * sin(radians(lat))))
AS distance
FROM clinics
JOIN countries ON countries.id = clinics.country_id
LEFT JOIN clinics_services ON clinics.id = clinics_services.clinic_id
WHERE clinics_services.service_id = 1 OR clinics_services.service_id = 29
GROUP BY clinics.id
HAVING SUM(clinics_services.service_id = 1) > 0 AND SUM(clinics_services.service_id = 29) > 0
) AS distances
WHERE distance < 50000
ORDER BY distance ASC