作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 3 张 table :
诊所
id | name | description | lat | lng | opening_hours| logo | address | city | zip | phone_number | email | url | gmaps_link | marker | created_at | updated_at
服务
id | name | created_at | updated_at
CLINIC_SERVICES
clinic_id | service_id
此查询应该返回两个结果,但目前它返回 0:
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 AND clinics_services.service_id = 29
GROUP BY clinics.id
) AS distances
WHERE distance < 50000
ORDER BY distance ASC
如果我输入OR而不是AND,我会得到5个诊所,这实际上按照我认为应该的方式工作。我怎样才能得到正确的结果(诊所有这两种服务)?我在这里做错了什么?
最佳答案
根据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
关于MySQL - WHERE/AND 与数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52058245/
我是一名优秀的程序员,十分优秀!