国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據庫 > MySQL > 正文

mysql 搜尋附近N公里內數(shù)據的簡單實例

2024-07-24 12:52:02
字體:
來源:轉載
供稿:網友

根據圓周率和地球半徑系數(shù)以及搜尋點的經緯度,搜尋數(shù)據表中與搜尋點之間的距離為N公里內的數(shù)據。

1、創(chuàng)建測試表

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、插入測試數(shù)據

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公里內的數(shù)據

搜尋點坐標:時代廣場 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;

執(zhí)行查詢:

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公里內數(shù)據的簡單實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持錯新站長站。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 桐柏县| 县级市| 托里县| 宽城| 雅安市| 云浮市| 太湖县| 韶关市| 宽甸| 涿鹿县| 延川县| 凌源市| 柘城县| 和林格尔县| 汉沽区| 南木林县| 柏乡县| 宁晋县| 炎陵县| 岳阳市| 渑池县| 花莲县| 揭西县| 武强县| 电白县| 武山县| 泸定县| 岐山县| 临潭县| 孝义市| 三门县| 万载县| 金乡县| 宝应县| 莲花县| 大厂| 临沭县| 崇阳县| 武功县| 博乐市| 通州市|