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

Vue uses vue meta info to set the title and meta information of each page

title: vue uses vue-meta-info to set the title an...

Detailed tutorial on installing Docker on Windows

Since my local MySQL version is relatively low, I...

Notes on Using Textarea

Why mention textarea specifically? Because the tex...

How to enter and exit the Docker container

1 Start the Docker service First you need to know...

Implementing a table scrolling carousel effect through CSS animation

An application of CSS animation, with the same co...

How to install git on linux

1. Introduction Git is a free, open source distri...

Some properties in CSS are preceded by "*" or "_".

Some properties in CSS are preceded by "*&qu...

Vue uses vue-quill-editor rich text editor and uploads pictures to the server

Table of contents 1. Preparation 2. Define the gl...

Examples of using the ES6 spread operator

Table of contents What are spread and rest operat...

MySQL data aggregation and grouping

We often need to summarize data without actually ...

Solve the problem of Docker starting Elasticsearch7.x and reporting an error

Using the Docker run command docker run -d -p 920...

Linux IO multiplexing epoll network programming

Preface This chapter uses basic Linux functions a...

MySQL quick recovery solution based on time point

The reason for writing such an article is that on...

How to display texture at the position of swipe in CocosCreator

Table of contents 1. Project requirements 2. Docu...