从数据库中有多个位置的数据库返回最近的位置
returning the closest locations from a database with multiple locations in database
您好,我正在尝试 return 半径 100 米内的最近位置。
公式中的输入应 return 最近的位置接近 100 米,而不是 return 相同的距离。
CREATE TABLE locations (lat decimal, lng decimal);
INSERT INTO locations (lat, lng) VALUES (47.0642512, 15.4696701),(47.0642630, 15.4696744) ;
WITH dt AS
(SELECT (
((acos(sin((47.0722487 * pi() / 180)) * sin((lat * pi() / 180)) + cos((47.0722487 * pi() / 180)) * cos((lat * pi() / 180)) * cos(((15.4477908 - lng) * pi() / 180)))) * 180 / pi()) * 60 * 1.1515 * 1.609344
) AS distance
FROM locations)
SELECT * FROM dt WHERE distance <= 100;
https://www.db-fiddle.com/f/3SpLAoG55wrEdnCisvYbrK/3
是我所在的地方运行吧
有人可以帮我吗?
使用st_distance_sphere
这很简单,
其他实例非常适合您的 100 米极限。
架构 (MySQL v8.0)
CREATE TABLE locations (lat double precision, lng double precision);
INSERT INTO locations (lat, lng) VALUES (49.227747, -122.994726)
,(47.0642512, 15.4696701)
,(47.0642630, 15.4696744);
查询#1
SELECT
*
FROM
locations
WHERE st_distance_sphere(POINT(-122.995842,49.227439 ), POINT( lng ,lat )) <= 100;
| lat | lng |
| --------- | ----------- |
| 49.227747 | -122.994726 |
查询#2
SELECT
st_distance_sphere(POINT(-122.995842,49.227439 ), POINT( lng ,lat ))
FROM
locations ;
| st_distance_sphere(POINT(-122.995842,49.227439 ), POINT( lng ,lat )) |
| --------------------------------------------------------------------- |
| 87.97942831882659 |
| 8584909.968344273 |
| 8584908.937326042
|
您好,我正在尝试 return 半径 100 米内的最近位置。 公式中的输入应 return 最近的位置接近 100 米,而不是 return 相同的距离。
CREATE TABLE locations (lat decimal, lng decimal);
INSERT INTO locations (lat, lng) VALUES (47.0642512, 15.4696701),(47.0642630, 15.4696744) ;
WITH dt AS
(SELECT (
((acos(sin((47.0722487 * pi() / 180)) * sin((lat * pi() / 180)) + cos((47.0722487 * pi() / 180)) * cos((lat * pi() / 180)) * cos(((15.4477908 - lng) * pi() / 180)))) * 180 / pi()) * 60 * 1.1515 * 1.609344
) AS distance
FROM locations)
SELECT * FROM dt WHERE distance <= 100;
https://www.db-fiddle.com/f/3SpLAoG55wrEdnCisvYbrK/3
是我所在的地方运行吧
有人可以帮我吗?
使用st_distance_sphere 这很简单,
其他实例非常适合您的 100 米极限。
架构 (MySQL v8.0)
CREATE TABLE locations (lat double precision, lng double precision);
INSERT INTO locations (lat, lng) VALUES (49.227747, -122.994726)
,(47.0642512, 15.4696701)
,(47.0642630, 15.4696744);
查询#1
SELECT
*
FROM
locations
WHERE st_distance_sphere(POINT(-122.995842,49.227439 ), POINT( lng ,lat )) <= 100;
| lat | lng |
| --------- | ----------- |
| 49.227747 | -122.994726 |
查询#2
SELECT
st_distance_sphere(POINT(-122.995842,49.227439 ), POINT( lng ,lat ))
FROM
locations ;
| st_distance_sphere(POINT(-122.995842,49.227439 ), POINT( lng ,lat )) |
| --------------------------------------------------------------------- |
| 87.97942831882659 |
| 8584909.968344273 |
| 8584908.937326042
|