将两个查询组合成单半径搜索以进行映射

时间:2014-12-15 11:49:30

标签: mysql

如果可能的话,我需要一些帮助将这些查询合并为一个。用户将在from中输入邮政编码。返回应该是邮政编码和距离。

表格结构:

zipcode |州|纬度|经度

SELECT latitude,
        longitude
 INTO   @latitude, @longitude
 FROM   zipcodes
 WHERE  `zipcode` = 36101
 LIMIT  1;

SELECT zipcode,
       ( 3959 * Acos(Cos(Radians(@latitude)) * Cos(Radians(latitude)) * Cos(
                                   Radians(longitude) - Radians(@longitude)) +
                              Sin(Radians(@latitude)) * Sin(Radians(latitude)))
       ) AS
       distance
FROM   zipcodes
HAVING distance < 10
ORDER  BY distance

我的解决方案 我最后只是做了两个单独的查询。

1 个答案:

答案 0 :(得分:0)

请尝试,如果你可能这些方法:

SELECT zipcode,
       ( 3959 * Acos(Cos(Radians(A.sel_latitude)) * Cos(Radians(latitude)) * Cos(
                                   Radians(longitude) - Radians(A.sel_longitude)) +
                              Sin(Radians(A.sel_latitude)) * Sin(Radians(latitude)))
       ) AS
       distance
FROM   
    zipcodes,
    (
       SELECT 
          sel_latitude,
          sel_longitude
       FROM   
          zipcodes
       WHERE  
          `zipcode` = 36101
       LIMIT  1
    ) AS A
HAVING distance < 10
ORDER  BY distance;

我希望它在某种程度上有所帮助