当前位置:网站首页>Mysql根据经纬度查询半径多少以内的数据,画个圈圈查数据库
Mysql根据经纬度查询半径多少以内的数据,画个圈圈查数据库
2022-06-30 12:18:00 【霸道卡东东】
在地图上画个圈圈,那么就有一个点和半径,点是有经纬度的,根据经度、维度、半径到mysql数据库中去查询符合这个半径范围内的数据。
下面这个SQL是一个函数,传入两个点,返回两点的距离,通过这个函数可以带入到SQL语句中查询,可是这样有个问题,如果数据量太大,那么就用不了索引,查询会非常慢
CREATE DEFINER=`root`@`%` FUNCTION `fnGetDistance`(`longitude1` DECIMAL(18,8),`latitude1` DECIMAL(18,8),`longitude2` DECIMAL(18,8),`latitude2` DECIMAL(18,8)) RETURNS bigint(20)
BEGIN
DECLARE Distance REAL DEFAULT 0;
SET Distance = ROUND(6378.137*2*ASIN(SQRT(POW(SIN((latitude1*PI()/180-latitude2*PI()/180)/2),2)+COS(latitude1*PI()/180)*COS(latitude2*PI()/180)*POW(SIN((longitude1*PI()/180-longitude2*PI()/180)/2),2)))*1000);
RETURN Distance;
END使用例子:由于我数据列大,非常慢,所以不采取
SELECT * FROM bg_building_price WHERE fnGetDistance(@longitude,@latitude,longitude,latitude)<[email protected]如果要想快,只能把计算方法直接放条件上,这样就能使索引生效,不通过函数:
SELECT * FROM bg_building_price WHERE (6378.137*2*ASIN(SQRT(POW(SIN(((@latitude-latitude)*PI()/180)/2),2)+COS(@latitude*PI()/180)*COS(latitude*PI()/180)*POW(SIN(((@longitude-longitude)*PI()/180)/2),2)))) < @radius补充:sql语句中@符号后表示变量,填写你地图上画圈圈的经纬度和半径。
边栏推荐
- [one day learning awk] use of built-in variables
- Substrate 源码追新导读: Call调用索引化, 存储层事物化全面完成
- Spatiotemporal prediction 2-gcn_ LSTM
- 【OpenGL】OpenGL Examples
- SuperMap iclient3d for webgl loading TMS tiles
- The realization of QT the flipping effect of QQ weather forecast window
- Lichuang EDA learning notes 10 common connector component identification and passive buzzer driving circuit
- "Xiaodeng" user personal data management in operation and maintenance
- zabbix-server启动失败处理方式
- FlinkSQL自定义UDAF使用的三种方式
猜你喜欢

60 个神级 VS Code 插件!!

Basic interview questions for Software Test Engineers (required for fresh students and test dishes) the most basic interview questions

90. (cesium chapter) cesium high level listening events

Visual Studio配置Qt并通过NSIS实现项目打包

【一天学awk】运算符

Q-learning notes

【C语言深度解剖】float变量在内存中存储原理&&指针变量与“零值”比较

江西财经大学智慧江财登录分析

Visual studio configures QT and implements project packaging through NSIS

如何利用AI技术优化独立站客服系统?听听专家怎么说!
随机推荐
How to use AI technology to optimize the independent station customer service system? Listen to the experts!
Redis - problèmes de cache
[one day learning awk] use of built-in variables
Tencent two sides: @bean and @component are used on the same class. What happens?
QT implementation dynamic navigation bar
【惊了】迅雷下载速度竟然比不上虚拟机中的下载速度
视频按每100帧存一个文件夹,处理完再图片转视频
数据仓库建设之确定主题域
Introduction to the novelty of substrat source code: indexing of call calls and fully completing the materialization of storage layer
Terms related to JMeter performance test and performance test passing standards
SQLSERVER 查询编码是 936 简体中文GBK,那我是写936 还是写GBK?
【一天学awk】基础中的基础
MySQL composite query
Swagger2 automatically generates API documents
MySQL中变量的定义和变量的赋值使用
grep匹配查找
JMeter性能测试之相关术语及性能测试通过标准
Q-learning notes
Efficient elliptic curve point addition and multiplication in scrypt
Google refutes rumors and gives up tensorflow. It's still alive!