2011-05-12 79 views
0

當前,我正在運行此SQL查詢以檢索商店定位器的自定義帖子。一切都很好,除非我希望它返回項目,即使「電話」字段爲空。截至目前只會拉那些該領域填補了那些即使字段爲空,您如何運行SQL查詢返回

這是運行時我的查詢的樣子:

my query

任何有線索? (也就是有一個更有效的方式來運行這個?)

SELECT wp_posts.post_title as name, 

     address.meta_value as address, 
     latitude.meta_value as lat, 
     longitude.meta_value as lng, 
     telephone.meta_value as phone, 


     (3959 * acos(
     cos(radians('%s')) * 
     cos(radians(CONVERT(latitude.meta_value, DECIMAL(10, 6)))) * 
     cos(radians(CONVERT(longitude.meta_value, DECIMAL(10, 6))) - radians('%s')) + 
     sin(radians('%s')) * sin(radians(CONVERT(latitude.meta_value, DECIMAL(10, 6)))) 
     )) AS distance 

     FROM wp_postmeta as address, wp_postmeta as latitude, wp_postmeta as longitude, wp_postmeta as telephone, 
     wp_posts 

WHERE 
    (wp_posts.ID = address.post_id 
     AND address.meta_key = '_dealer_address') 

AND  (wp_posts.ID = latitude.post_id 
     AND latitude.meta_key = '_dealer_latitude') 

AND  (wp_posts.ID = longitude.post_id 
     AND longitude.meta_key = '_dealer_longitude')  

AND  (wp_posts.ID = telephone.post_id 
     AND telephone.meta_key = '_dealer_telephone') 

回答

2

根據Marc B的評論發佈答案。

 SELECT wp_posts.post_title as name, 
address.meta_value as address, 
latitude.meta_value as lat, 
longitude.meta_value as lng, 
telephone.meta_value as telephone, 

    (3959 * acos(
    cos(radians('%s')) * 
    cos(radians(CONVERT(latitude.meta_value, DECIMAL(10, 6)))) * 
    cos(radians(CONVERT(longitude.meta_value, DECIMAL(10, 6))) - radians('%s')) + 
    sin(radians('%s')) * sin(radians(CONVERT(latitude.meta_value, DECIMAL(10, 6)))) 
    )) AS distance 

FROM wp_posts 
LEFT JOIN wp_postmeta AS address ON(
wp_posts.ID = address.post_id 
AND address.meta_key = '_dealer_address' 
) 
LEFT JOIN wp_postmeta AS latitude ON(
wp_posts.ID = latitude.post_id 
AND latitude.meta_key = '_dealer_latitude' 
) 
LEFT JOIN wp_postmeta AS longitude ON(
wp_posts.ID = longitude.post_id 
AND longitude.meta_key = '_dealer_longitude' 
) 
LEFT JOIN wp_postmeta AS telephone ON(
wp_posts.ID = telephone.post_id 
AND telephone.meta_key = '_dealer_telephone' 
) 
WHERE wp_posts.post_type = 'dealers' HAVING distance < '%s' ORDER BY distance LIMIT 0 ,   20 
0

對於你最終AND語句,只需添加一個OR條件NULL

AND  (wp_posts.ID = telephone.post_id 
     AND telephone.meta_key IS NULL OR telephone.meta_key = '_dealer_telephone') 
+3

或重寫使用電話的東西,而不是一個內部聯接左連接。 – 2011-05-12 19:15:37

+0

Marc B--我已經成功地使用了你的方法,如果你把這個作爲n答案重新發布,我會設置爲接受的答案。謝謝! – Joe 2011-05-12 21:17:37

0

試試這個:

SELECT wp_posts.post_title as name,   
address.meta_value as address,  
    latitude.meta_value as lat,   
longitude.meta_value as lng,  
    telephone.meta_value as phone,  
     (3959 * 
acos(  cos(radians('%s')) *  
    cos(radians(CONVERT(latitude.meta_value, DECIMAL(10, 6)))) *  
    cos(radians(CONVERT(longitude.meta_value, DECIMAL(10, 6))) - radians('%s')) + sin(radians('%s')) * sin(radians(CONVERT(latitude.meta_value, DECIMAL(10, 6))))   )) AS distance   
FROM wp_postmeta as address, 
wp_postmeta as latitude, 
wp_postmeta as longitude, 
wp_postmeta as telephone left outer join  
     wp_posts on (wp_posts.ID = telephone.post_id AND telephone.meta_key = '_dealer_telephone') 
WHERE  (wp_posts.ID = address.post_id  AND address.meta_key = '_dealer_address')  
AND  (wp_posts.ID = latitude.post_id   AND latitude.meta_key = '_dealer_latitude') 
AND  (wp_posts.ID = longitude.post_id   AND longitude.meta_key = '_dealer_longitude') 
+0

嗯,這給出了與我原來的查詢相同的結果(不會拉空帖子的帖子)。 – Joe 2011-05-12 20:02:52