Home > Database > Mysql Tutorial > How does MySQL calculate the distance between two coordinates and sort them?

How does MySQL calculate the distance between two coordinates and sort them?

藏色散人
Release: 2020-05-31 13:47:26
forward
2536 people have browsed it

How does MySQL calculate the distance between two coordinates and sort them?

Environment

MySQL5.6

https://dev.mysql.com/doc/refman/5.6/en/spatial-relation-functions-object-shapes.html#function_st-distance
Copy after login

Table structure and data

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;
Copy after login
rrree

Query method

(Taking Hohhot City, Inner Mongolia Autonomous Region as the calculation center)

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);
Copy after login

Query results

How does MySQL calculate the distance between two coordinates and sort them?

Recommended: "mysql tutorial"

The above is the detailed content of How does MySQL calculate the distance between two coordinates and sort them?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template