我使用Haversine formulaSQL語法錯誤 - haversine公式
我的表結構的WordPress數據庫試圖get nearest places下面
帖子
+--------------+
| Field |
+--------------+
| ID |
| post_author |
| post_title |
| post_type |
+--------------+
postmeta
+--------------+
| Field |
+--------------+
| meta_id |
| post_id |
| meta_key |
| meta_value |
+--------------+
條
,並有記錄與meta_key
值latitude
和longitude
見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工作臺,但它並沒有括號匹配:-( –
@mithun做這個查詢提供你的結果。 – 2015-01-06 15:17:59