mysql 搜寻附近N公里内数据的简单实例
网络编程 2021-07-05 14:36www.168986.cn编程入门
狼蚁网站SEO优化长沙网络推广就为大家带来一篇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 = RAros(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公里内数据的简单实例就是长沙网络推广分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持狼蚁SEO。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程