mysql附近搜索_mysql 搜索附近数据实例解析

mysql 搜索附近数据实例解析

发布时间:2020-04-28 14:14:08

来源:亿速云

阅读:137

作者:三月

本文主要给大家介绍mysql 搜索附近数据实例解析,希望可以给大家补充和更新些知识,如有其它问题需要了解的可以持续在亿速云行业资讯里面关注我的更新文章的。

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.插入测试数据

22b42f5ae6907bebd1efc30e8d249cd4.pnginsert 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公里内数据的实例相关内容,更多相关知识请关注亿速云。

相关推荐:

mysql 连接闪断自动重连的方法

php 实现HTML实体编号与非ASCII字符串相互转换类

php 根据自增id创建唯一编号类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值