1
我知道這是不對的,雖然這是工作。子查詢與WordPress的帖子和post_meta
我需要查找距離給定半徑和位置較近的所有帖子(作爲收入),但我需要子查詢post_meta中的經度和緯度信息。
這會傷害你的眼睛,所以這裏是:
/*
lat -23.6480147
lng -46.704116399999975
radius 20km
6371 para km e 3959 para miles
*/
SELECT vision_outlet.wp_posts.ID as 'ID',
vision_outlet.wp_posts.post_title as 'Nome',
(
SELECT vision_outlet.wp_postmeta.meta_value
FROM vision_outlet.wp_postmeta
WHERE vision_outlet.wp_postmeta.meta_key = 'formatted_address'
AND vision_outlet.wp_postmeta.post_id = vision_outlet.wp_posts.ID
) as 'formatted_address' ,
(
SELECT vision_outlet.wp_postmeta.meta_value
FROM vision_outlet.wp_postmeta
WHERE vision_outlet.wp_postmeta.meta_key = 'lat'
AND vision_outlet.wp_postmeta.post_id = vision_outlet.wp_posts.ID
) as 'lat',
(
SELECT vision_outlet.wp_postmeta.meta_value
FROM vision_outlet.wp_postmeta
WHERE vision_outlet.wp_postmeta.meta_key = 'lng'
AND vision_outlet.wp_postmeta.post_id = vision_outlet.wp_posts.ID
) as 'lng',
(
6371 * acos(cos(radians(-23.6480147)) * cos(radians
(
(
SELECT vision_outlet.wp_postmeta.meta_value
FROM vision_outlet.wp_postmeta
WHERE vision_outlet.wp_postmeta.meta_key = 'lat'
AND vision_outlet.wp_postmeta.post_id = vision_outlet.wp_posts.ID
)
)) * cos(radians
(
(
SELECT vision_outlet.wp_postmeta.meta_value
FROM vision_outlet.wp_postmeta
WHERE vision_outlet.wp_postmeta.meta_key = 'lng'
AND vision_outlet.wp_postmeta.post_id = vision_outlet.wp_posts.ID
)
) - radians(-46.704116399999975)) + sin(radians(-23.6480147)) * sin(radians
(
(
SELECT vision_outlet.wp_postmeta.meta_value
FROM vision_outlet.wp_postmeta
WHERE vision_outlet.wp_postmeta.meta_key = 'lat'
AND vision_outlet.wp_postmeta.post_id = vision_outlet.wp_posts.ID
)
)))
) as 'distance'
FROM vision_outlet.wp_posts, vision_outlet.wp_postmeta
WHERE vision_outlet.wp_posts.ID = vision_outlet.wp_postmeta.post_id
AND vision_outlet.wp_posts.post_status = 'publish'
AND vision_outlet.wp_postmeta.meta_key = 'formatted_address'
HAVING distance < 20
ORDER BY distance
的問題是:我是否需要考慮創建一個新的表或有任何重構這種方式?
我覺得元查詢是針對這個問題http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters –
FWIW,當遇到這種問題有用的,我喜歡構造一個僞規範化的物化視圖,然後在其上執行其餘的查詢,但這可能不是最優的。 – Strawberry