gpt4 book ai didi

mysql - MariaDB 没有 ST_Distance_Sphere 的等效函数

转载 作者:行者123 更新时间:2023-11-29 15:21:10 26 4
gpt4 key购买 nike

在 MySQL 5.7 中,有一个名为 ST_Distance_Sphere 的函数,可用于查找半径内的点,例如:

CREATE TABLE `places` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(256) DEFAULT NULL,
`coordinates` point DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


INSERT INTO `places` ( `name`, `coordinates`)
VALUES ("Eiffel Tower", POINT(48.858271, 2.293795));

INSERT INTO `places` ( `name`, `coordinates`)
VALUES ("Pere Lachaise", POINT(48.861131, 2.394683));

INSERT INTO `places` ( `name`, `coordinates`)
VALUES ("Brooklyn", POINT(40.711089, -73.948391));

如果我们想查看距离巴黎市中心卢浮宫不到10公里的所有点,我们可以这样做:

SELECT name FROM places
WHERE ST_Distance_Sphere(coordinates, POINT(48.861105, 2.335337)) < 10000

但是 MariaDB 10.1+ 中不支持此类函数

如何在 MariaDB 中重新创建相同的函数或重新创建相同的查询。以下帖子展示了重新创建相同函数的一种方法 FUNCTION ST_Distance_Sphere does not exist in MariaDB

也许是这样的?

SELECT name FROM places
WHERE ST_Within(
coordinates, ST_Buffer(POINT(48.861105, 2.335337), 10000)
);

感谢任何帮助。

最佳答案

基于@adam78和@Rick James的建议我是这样解决的:

CREATE FUNCTION st_distance_sphere(pt1 POINT, pt2 POINT)
RETURNS double(10,2)

RETURN 6371000 * 2 * ASIN(
SQRT(
POWER(SIN((ST_Y(pt2) - ST_Y(pt1)) * pi()/180 / 2), 2) +
COS(ST_Y(pt1) * pi()/180 ) *
COS(ST_Y(pt2) * pi()/180) *
POWER(SIN((ST_X(pt2) - ST_X(pt1)) * pi()/180 / 2), 2)
)
)

关于mysql - MariaDB 没有 ST_Distance_Sphere 的等效函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59370894/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com