如何摆脱 HAVING 子句中的非分组字段

发布于 2024-08-05 07:58:11 字数 362 浏览 5 评论 0原文

ONLY_FULL_GROUP_BY 模式下执行时,执行以下查询时收到错误“HAVING 子句中使用了非分组字段“距离””。该查询统计某个纬度和距离 15 公里范围内的酒店数量。经度。有没有办法重写此查询,以便我在 ONLY_FULL_GROUP_BY 模式下不再收到错误?

SELECT count(id) as total, (foo * 100) AS 'distance'
FROM `hotels` 
WHERE `lng` between 4.56 and 5.08 and `lat` between 52.22 and 52.65 
HAVING `distance` < 15

When executing in ONLY_FULL_GROUP_BY mode, I get the error "non-grouping field 'distance' is used in HAVING clause" when executing the following query. The query counts the amount of hotels that are within 15 km distance of a certain latitude & longitude. Is there a way to rewrite this query so I don't get the error anymore in ONLY_FULL_GROUP_BY mode?

SELECT count(id) as total, (foo * 100) AS 'distance'
FROM `hotels` 
WHERE `lng` between 4.56 and 5.08 and `lat` between 52.22 and 52.65 
HAVING `distance` < 15

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活雷疯 2024-08-12 07:58:11

将 HAVING 更改为附加 WHERE 子句:

WHERE `lng` BETWEEN 4.56 AND 5.08 AND `lat` BETWEEN 52.22 AND 52.65 
AND `distance` < 15

我不确定 MySQL 是否支持在 WHERE 子句中使用别名列名(在本例中为 distance)。如果不是,只需将其更改为:

AND (foo * 100) < 15

Change the HAVING to an additional WHERE clause:

WHERE `lng` BETWEEN 4.56 AND 5.08 AND `lat` BETWEEN 52.22 AND 52.65 
AND `distance` < 15

I'm not sure if MySQL supports using the aliased column name in the WHERE clause (distance in this case). If not, just change it to:

AND (foo * 100) < 15
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文