mysql 附近3公里的_mysql搜寻附近N公里内数据的实例
根據圓周率和地球半徑系數以及搜尋點的經緯度,搜尋數據表中與搜尋點之間的距離為N公里內的數據。
1.創建測試表CREATE TABLE `location` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, `longitude` decimal(13,10) NOT NULL, `latitude` decimal(13,10) NOT NULL, PRIMARY KEY (`id`), KEY `long_lat_index` (`longitude`,`latitude`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.插入測試數據insert into location(name,longitude,latitude) values
('廣州東站',113.332264,23.156206),
('林和西',113.330611,23.147234),
('天平架',113.328095,23.165376);mysql> select * from `location`;
+----+--------------+----------------+---------------+| id | name | longitude | latitude |
+----+--------------+----------------+---------------+| 1 | 廣州東站 | 113.3322640000 | 23.1562060000 |
| 2 | 林和西 | 113.3306110000 | 23.1472340000 || 3 | 天平架 | 113.3280950000 | 23.1653760000 |
+----+--------------+----------------+---------------+
3.搜尋1公里內的數據
搜尋點坐標:時代廣場 113.323568, 23.146436
6370.996公里為地球的半徑
計算球面兩點坐標距離公式C = sin(MLatA)sin(MLatB)cos(MLonA-MLonB) + cos(MLatA)cos(MLatB)
Distance = RArccos(C)*Pi180
根據計算公式得到查詢語句如下:select * from `location` where (
acos(sin(([#latitude#]*3.1415)/180) * sin((latitude*3.1415)/180) + cos(([#latitude#]*3.1415)/180) * cos((latitude*3.1415)/180) * cos(([#longitude#]*3.1415)/180 - (longitude*3.1415)/180))*6370.996)<=1;
執行查詢:mysql> select * from `location` where ( -> acos( -> sin((23.146436*3.1415)/180) * sin((latitude*3.1415)/180) + -> cos((23.146436*3.1415)/180) * cos((latitude*3.1415)/180) * cos((113.323568*3.1415)/180 - (longitude*3.1415)/180) -> )*6370.996 -> )<=1;
+----+-----------+----------------+---------------+| id | name | longitude | latitude |
+----+-----------+----------------+---------------+| 2 | 林和西 | 113.3306110000 | 23.1472340000 |
+----+-----------+----------------+---------------+
本文講解了mysql 搜尋附近N公里內數據的實例相關內容,更多相關知識請關注Gxl網。
相關推薦:
mysql 連接閃斷自動重連的方法
php 實現HTML實體編號與非ASCII字符串相互轉換類
php 根據自增id創建唯一編號類
總結
以上是生活随笔為你收集整理的mysql 附近3公里的_mysql搜寻附近N公里内数据的实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 整数逆位运算_python
- 下一篇: 上海欢乐谷上海滩演出时间