【问题标题】:SQL syntax error - Haversine formulaSQL 语法错误 - Haversine 公式
【发布时间】:2012-10-26 09:11:37
【问题描述】:

我正在尝试使用 Haversine formula 从 WordPress 数据库中 get nearest places

下面是我的表结构

帖子

+--------------+
| Field        |
+--------------+
| ID           |
| post_author  |
| post_title   | 
| post_type    | 
+--------------+

postmeta

+--------------+
| Field        |
+--------------+
| meta_id      |
| post_id      |
| meta_key     |
| meta_value   |
+--------------+

并拥有meta_keylatitudelongitude 的记录

查看answer to my previous question 获取我用来获取纬度和经度的 SQL。

SELECT p.ID, 
  p.post_title, 
  p.post_author,
  max(case when pm.meta_key='latitude' then pm.meta_value end) latitude,
  max(case when pm.meta_key='longitude' then pm.meta_value end) longitude
FROM `wp_posts` p
LEFT JOIN `wp_postmeta` pm
  on p.ID=pm.post_id 
WHERE p.post_type='place' 
  AND (pm.meta_key='latitude' OR pm.meta_key='longitude') 
GROUP BY p.ID, p.post_title, p.post_author
ORDER BY p.ID ASC

现在我想将上面的查询合并到answer for this question

SELECT item1, item2, 
    ( 3959 * acos( cos( radians(37) ) 
                   * cos( radians( lat ) ) 
                   * cos( radians( lng ) 
                       - radians(-122) ) 
                   + sin( radians(37) ) 
                   * sin( radians( lat ) ) 
                 )
   ) AS distance 
FROM geocodeTable 
HAVING distance < 25 
ORDER BY distance LIMIT 0 , 20;

下面是组合查询

SELECT ID, 
  post_title, 
  post_author,
  max(case when meta_key='latitude' then meta_value end) latitude,
  max(case when meta_key='longitude' then meta_value end) longitude,
  ( 3959 * acos( cos( radians(18.204540500000) ) 
                   * cos( radians( latitude ) ) 
                   * cos( radians( longitude ) 
                       - radians(-66.450958500000) ) 
                   + sin( radians(18.204540500000 ) 
                   * sin( radians( latitude ) ) 
                 )
   ) AS distance 
FROM `wp_posts` 
LEFT JOIN `wp_postmeta` 
  on ID=post_id 
WHERE post_type='place' 
  AND (meta_key='latitude' OR meta_key='longitude') 
GROUP BY ID, post_title, post_author
ORDER BY ID ASC

但这会产生语法错误

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS distance FROM `wp_posts` LEFT JOIN `wp_postmeta` on ID=post_id WHERE po' at line 13

【问题讨论】:

    标签: mysql sql join


    【解决方案1】:

    您缺少第一个 sin() 的结束 )

    ( 3959 * acos( cos( radians(18.204540500000) ) 
                       * cos( radians( latitude ) ) 
                       * cos( radians( longitude ) 
                           - radians(-66.450958500000) ) 
                       + sin( radians(18.204540500000 ) ) /* <--- here */
                       * sin( radians( latitude ) ) 
                  )
     ) AS distance 
    

    虽然很难从视觉上发现,但我通过将您的代码复制到支持大括号匹配的文本编辑器中发现了这一点。强烈建议使用一个,如果不是用于查询开发和测试,那么至少用于调试。

    【讨论】:

    • 我使用的是 MySQL Workbench,但它没有大括号匹配 :-(
    • @mithun 此查询是否为您提供结果。
    【解决方案2】:

    试试这个:

    SELECT *, ( 3959 * ACOS( COS( RADIANS(18.204540500000) ) 
                       * COS( RADIANS( latitude ) ) 
                       * COS( RADIANS( longitude ) 
                           - RADIANS(-66.450958500000) ) 
                       + SIN( RADIANS(18.204540500000 ) )
                       * SIN( RADIANS( latitude ) ) 
                     )
       ) AS distance  
    FROM 
    (SELECT ID, 
      post_title, 
      post_author,
      MAX(CASE WHEN meta_key='geo_latitude' THEN meta_value END) latitude,
      MAX(CASE WHEN meta_key='geo_longitude' THEN meta_value END) longitude
    FROM `wp_posts` 
    LEFT JOIN `wp_postmeta` 
      ON ID=post_id 
    WHERE post_type='place' 
      AND (meta_key='geo_latitude' OR meta_key='geo_longitude') 
    GROUP BY ID, post_title, post_author
    ORDER BY ID ASC) AS A
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      • 2015-02-26
      • 2017-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多