mysql座標計算 MySQL計算兩座標距離並排序

2021-10-18 02:16:39 字數 1103 閱讀 6055

環境

mysql5.6

表結構及資料

drop table if exists `locationpoint`;

create table `locationpoint`

`id` int(11) not null auto_increment,

`province` varchar(20) not null,

`city` varchar(20) not null,

`longitude` double(10, 3) not null,

`latitude` double(10, 3) not null,

primary key (`id`)

) engine = innodb

auto_increment = 1156

default charset = utf8;

insert into `locationpoint`

values (1, '山東', '濟南', 116.938477, 36.597889),

(2, '河北', '石家莊', 114.477539, 38.030786),

(3, '浙江', '杭州', 120.058594, 30.334954),

(4, '河南', '鄭州', 113.629, 34.744),

(5, '安徽省', '合肥', 117.170, 31.520);

查詢方式

(以內蒙古自治區呼和浩特市為計算中心)

select id,

city,

longitude,

latitude,

round(

st_distance(

point(longitude, latitude),

point(111.621094, 40.913513)

) / 0.0111

) * 1000

as distance

from locationpoint

order by distance;

查詢結果

mysql 座標查詢計算距離

6378.138這個是地球的直徑,單位千公尺.latitude是使用者位置的緯度,longitude是使用者位置經度.latitude 為商戶的緯度字段,longitude為商戶的經度字段。上面一段sql計算得出根據使用者經緯度計算與商戶的距離。30.5821398542,select round ...

MySQL計算兩座標之間的距離

表結構及資料 drop table ifexists tb locationpoint create table tb locationpoint id int 11 notnull auto increment province varchar 20 not null comment 省份 cit...

mysql 生成執行計畫 MySQL的執行計畫

mysql的執行計畫 什麼是執行計畫?執行計畫通常是開發者優化sql語句的第一步。mysql在解析sql語句時,會生成多套執行方案,然後內部會進行乙個成本的計算,然後通過優化器選擇乙個最優的方案執行,然後根據這個方案會生成乙個執行計畫。開發者通過檢視sql語句的執行計畫,可以直觀的了解到mysql是...