How to use MySQL's geometry type to handle longitude and latitude distance problems

How to use MySQL's geometry type to handle longitude and latitude distance problems

Create a table

CREATE TABLE `map` (
 `id` int(11) NOT NULL,
 `address` varchar(255) NOT NULL DEFAULT '',
 `location` geometry NOT NULL,
 PRIMARY KEY (`id`),
 SPATIAL KEY `idx_location` (`location`)
)

insert

INSERT INTO map (id, address, location) VALUES (1, 'somewhere', ST_GeomFromText('POINT(121.366961 31.190049)'));

Note that you must use the ST_GeomFromText function, and POINT() contains: longitude + space + latitude

Query

1. Check the latitude and longitude

SELECT address, ST_AsText(location) AS location FROM map;

2. Calculate the distance between two points

SELECT ST_Distance_Sphere(POINT(121.590347, 31.388094),location) AS distant FROM map;

The calculated result is in meters.

Note that the latitude and longitude in POINT() are now separated by commas.

3. Search for locations within 1000m and sort them from far to near

Copy the code as follows:
SELECT id, address, ST_Distance_Sphere(POINT(121.590347, 31.388094),location) AS distant FROM map WHERE ST_Distance_Sphere(POINT(121.590347, 31.388094),location) < 1000 ORDER BY distant;

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Mybatis learning road mysql batch add data method
  • Detailed explanation of how to implement secondary cache with MySQL and Redis
  • In-depth explanation of MySQL common index and unique index
  • How to view mysql binlog (binary log)
  • Docker creates MySQL explanation
  • PHP date() format MySQL insert datetime method
  • How to solve SQL injection problem with pymysql
  • Steps to export the fields and related attributes of MySQL tables
  • Solution to the problem of MySQL thread in Opening tables
  • MySQL limit performance analysis and optimization

<<:  Solve the cross-domain problem of get and post requests of vue $http

>>:  Detailed explanation of how Tomcat implements asynchronous Servlet

Recommend

Methods and techniques for designing an interesting website (picture)

Have you ever encountered a situation where we hav...

How to use Volume to transfer files between host and Docker container

I have previously written an article about file t...

Docker View JVM Memory Usage

1. Enter the host machine of the docker container...

CSS scroll-snap scroll event stop and element position detection implementation

1. Scroll Snap is a must-have skill for front-end...

Limit input type (multiple methods)

1. Only Chinese characters can be input and pasted...

How to implement mysql database backup in golang

background Navicat is the best MySQL visualizatio...

The role and opening of MySQL slow query log

Preface The MySQL slow query log is a type of log...

Detailed explanation of Vue filter implementation and application scenarios

1. Brief Introduction Vue.js allows you to define...

Tomcat's class loading mechanism process and source code analysis

Table of contents Preface 1. Tomcat class loader ...

JS implements a detailed plan for the smooth version of the progress bar

The progress bar is not smooth I believe that mos...