4

下午,

我很难解决这个问题。我有一个 MySQL 表,其中列出了英国邮政编码及其经度和纬度值。我希望能够在表格上进行搜索,以找到最接近给定长/纬度对的邮政编码。

我一直在尝试使用的查询是:

"SELECT id, outcode AS thecode, @la := MATCH(lat) AGAINST(?) AS score_lat, @ln := MATCH(lng) AGAINST(?) AS score_lng, @la + @ln AS score_total FROM postcodes ORDER BY score_total DESC LIMIT 10

然而,这只会返回看似随机的邮政编码,例如 Lat: 55.775549 和 Long: -4.047556

Array
(
[0] => Array
    (
        [id] => 929
        [thecode] => FK14
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )

[1] => Array
    (
        [id] => 2785
        [thecode] => UB3
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )

[2] => Array
    (
        [id] => 993
        [thecode] => G70
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )

[3] => Array
    (
        [id] => 2849
        [thecode] => WC2B
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )

[4] => Array
    (
        [id] => 1057
        [thecode] => GU29
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )

[5] => Array
    (
        [id] => 2913
        [thecode] => WS13
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )

[6] => Array
    (
        [id] => 1121
        [thecode] => HP20
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )

[7] => Array
    (
        [id] => 1185
        [thecode] => IG6
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )

[8] => Array
    (
        [id] => 1249
        [thecode] => IV25
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )

[9] => Array
    (
        [id] => 1313
        [thecode] => KA8
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )
)

数据库的架构是:

CREATE TABLE `postcodes` (
  `id` int(11) NOT NULL auto_increment,
  `outcode` varchar(4) NOT NULL,
  `lat` varchar(20) NOT NULL,
  `lng` varchar(20) NOT NULL,
  PRIMARY KEY  (`id`),
  FULLTEXT KEY `lat` (`lat`),
  FULLTEXT KEY `lng` (`lng`)
) ENGINE=MyISAM AUTO_INCREMENT=2975 DEFAULT CHARSET=latin1 AUTO_INCREMENT=2975 ;

我希望有人能帮帮忙!如果您需要更多信息,请询问...

谢谢,

提示2尾

4

1 回答 1

5

MySQLMATCH()函数用于全文搜索以“匹配”字符串。(所以它返回零值也就不足为奇了。)

如果“最近”是指您要计算地图上两点之间的距离(以“乌鸦飞”的方式测量),坐标以(十进制度)纬度和经度为单位,您真的需要使用大圆距离(GCD)计算。

http://en.wikipedia.org/wiki/Great-circle_distance

你可以跳过所有这些血淋淋的细节,直接使用我的实现。下面是我的一条 SQL 语句的 SELECT 列表的摘录,这个表达式计算两点之间的距离(以英里为单位)......

     , ACOS(
          COS(RADIANS( d2.latitude ))
        * COS(RADIANS( d1.latitude ))
        * COS(RADIANS( d2.longitude ) - RADIANS( d1.longitude ))
        + SIN(RADIANS( d2.latitude ))
        * SIN(RADIANS( d1.latitude ))
           )*3958.82 AS distance_miles

在本例中,d1代表起点,d2代表终点。和作为 DECIMAL 值提供latitudelongitude

有了一个“已知”点d1,我可以按这个表达式排序,首先获得“最接近”的点d2。(对于多个原点,我可以按d1.id,然后按此表达式排序,以获得每个最接近的d2第一个d1。但我的问题已经足够了......


我从您的问题中复制了查询并对其进行了修改(如下)。基本上,我删除了“分数”列,并将其替换为执行距离计算的表达式:

SELECT id
     , outcode AS thecode
     , ACOS(
           COS(RADIANS( d2.latitude ))
         * COS(RADIANS( @d1_latitude ))
         * COS(RADIANS( d2.longitude ) - RADIANS( @d1_longitude ))
         + SIN(RADIANS( d2.latitude ))
         * SIN(RADIANS( @d1_latitude ))
           )*3958.82 AS distance_miles
  FROM postcodes d2
  JOIN (SELECT @d1_latitude := ?, @d1_longitude := ?) v
 ORDER BY distance_miles LIMIT 10

在这种情况下,@d1_变量(从绑定变量分配)是“已知”点的纬度和经度。对于postcodes表中的每一行(为方便起见,我将其命名为别名d2),此表达式计算表中的纬度/经度与“已知”点之间的距离。

注意:内联视图别名 asv就在那里,因此您可以只绑定一次纬度,并将值分配给可以引用的用户变量。可以省略该内联视图,您可以看到需要将纬度绑定两次的位置。

注意:这以“英里”为单位计算距离。通过用不同的值代替3958.82常数,您可以轻松地获得以公里 (km) 为单位的距离。

注意:不需要返回距离;如果您只想按距离返回最近的 10 个,则可以将该表达式放在 ORDER BY 子句中,例如

SELECT id
     , outcode AS thecode
  FROM postcodes d2
  JOIN (SELECT @d1_latitude := ?, @d1_longitude := ?) v
 ORDER
    BY ACOS(
           COS(RADIANS( d2.latitude ))
         * COS(RADIANS( @d1_latitude ))
         * COS(RADIANS( d2.longitude ) - RADIANS( @d1_longitude ))
         + SIN(RADIANS( d2.latitude ))
         * SIN(RADIANS( @d1_latitude ))
           )*3958.82 AS distance_miles
 LIMIT 10

如果您正在寻找两点之间的距离以外的东西,请告诉我,因为在这种情况下,这个答案对您真的没有帮助。

于 2012-08-01T19:23:32.933 回答