MySQL查询失败,因为列表中的列名

发布于 2025-01-29 09:25:03 字数 1092 浏览 4 评论 0原文

我有一个MySQL查询,可以完美地工作,没有具有子句,但是当我使用该子句时,我会遇到错误,我认为这与我使用该子句进行比较有关用我在选择中声明的列名称。

查询:

SELECT
  DISTINCT p.ID,
  p.post_title,
  practice_latitude.meta_value as locLat,
  practice_longitude.meta_value as locLong,
  (
    '3959' * acos(
      cos(radians('39.40517956963392')) * cos(radians(practice_latitude.meta_value)) * cos(
        radians(practice_longitude.meta_value) - radians('-118.99713149999997')
      ) + sin(radians('39.40517956963392')) * sin(radians(practice_latitude.meta_value))
    )
  ) AS distance
FROM
  wp_posts p
  INNER JOIN wp_postmeta practice_latitude ON p.ID = practice_latitude.post_id
  INNER JOIN wp_postmeta practice_longitude ON p.ID = practice_longitude.post_id
WHERE
  p.post_type = 'awpcp_listing'
  AND p.post_status = 'publish'
  AND practice_latitude.meta_key = '_awpcp_extra_field[36]'
  AND practice_longitude.meta_key = '_awpcp_extra_field[37]'
HAVING
  distance < 500
group by
  practice_latitude.meta_value,
  practice_longitude.meta_value;

似乎在选择中声明的“距离”列是问题。我该如何纠正以应用适当的有子句 e,由于查询否则可以很好地工作?

I have a mysql query that works perfectly without the HAVING clause, but when I use that clause I get an error and I think it has to do with the fact that I'm using the clause for a comparison with a column name that I declare in my select.

The Query:

SELECT
  DISTINCT p.ID,
  p.post_title,
  practice_latitude.meta_value as locLat,
  practice_longitude.meta_value as locLong,
  (
    '3959' * acos(
      cos(radians('39.40517956963392')) * cos(radians(practice_latitude.meta_value)) * cos(
        radians(practice_longitude.meta_value) - radians('-118.99713149999997')
      ) + sin(radians('39.40517956963392')) * sin(radians(practice_latitude.meta_value))
    )
  ) AS distance
FROM
  wp_posts p
  INNER JOIN wp_postmeta practice_latitude ON p.ID = practice_latitude.post_id
  INNER JOIN wp_postmeta practice_longitude ON p.ID = practice_longitude.post_id
WHERE
  p.post_type = 'awpcp_listing'
  AND p.post_status = 'publish'
  AND practice_latitude.meta_key = '_awpcp_extra_field[36]'
  AND practice_longitude.meta_key = '_awpcp_extra_field[37]'
HAVING
  distance < 500
group by
  practice_latitude.meta_value,
  practice_longitude.meta_value;

It seems like the 'distance' column being declared in the select is the issue though. How can I correct that to apply the appropriate HAVING clause
e, since the query otherwise works perfectly?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文