我在解決這個問題時遇到了一些困難。我有一張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
然而,這只是返回似乎是隨機的郵政編碼,例如與緯度:55.775549和龍:-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 ;
我希望有人能幫助!如果您需要了解更多信息,請只問...
感謝,
tip2tail
你看過嗎:http://dev.mysql.com/doc/refman/5.0/en/spatial-extensions.html你的應用程序相當簡單,因爲它只是距離公式的基本應用程序,但你問的是一個空間問題。你在做什麼試圖匹配字符串,你實際要問的問題是一個距離問題。這種方法永遠不會有效。 – hsanders 2012-08-01 19:03:09
@hsanders我不知道該從哪裏開始!你能提供任何建議或例子,說明我可以如何實現我所需要的?謝謝tip2tail – tip2tail 2012-08-01 19:23:37