mysql 和 psql 语法的区别——寻找最近的坐标
The diffrence in mysql and psql syntax - finding the closest geocoordinates
我找到了一个 psql 代码,如下所示:
select * from (
SELECT *,( 3959 * acos( cos( radians(6.414478) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(12.466646) ) + sin( radians(6.414478) ) * sin( radians( lat ) ) ) ) AS distance
FROM station_location
) al
where distance < 5
ORDER BY distance
LIMIT 20;
问题是我完全不明白。
在我以前使用 mysql 之前,语法完全不同:
SELECT latitude, longitude, SQRT(
POW(69.1 * (latitude - [startlat]), 2) +
POW(69.1 * ([startlng] - longitude) * COS(latitude / 57.3), 2)) AS distance
FROM TableName HAVING distance < 25 ORDER BY distance;
Find nearest latitude/longitude with an SQL query
如何把这个mysql指令转换成psql,这样用户的经纬度就变成了startlng
和startlat
?
不评论计算本身,这个 PostgreSQL 的“端口”应该可以工作:
select * from (select SQRT(
POW(69.1 * (10 - 8), 2) +
POW(69.1 * (100 - 10) * COS(10 / 57.3), 2)) AS distance
from table) d where distance < 25 ORDER BY distance;
我找到了一个 psql 代码,如下所示:
select * from (
SELECT *,( 3959 * acos( cos( radians(6.414478) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(12.466646) ) + sin( radians(6.414478) ) * sin( radians( lat ) ) ) ) AS distance
FROM station_location
) al
where distance < 5
ORDER BY distance
LIMIT 20;
问题是我完全不明白。 在我以前使用 mysql 之前,语法完全不同:
SELECT latitude, longitude, SQRT(
POW(69.1 * (latitude - [startlat]), 2) +
POW(69.1 * ([startlng] - longitude) * COS(latitude / 57.3), 2)) AS distance
FROM TableName HAVING distance < 25 ORDER BY distance;
Find nearest latitude/longitude with an SQL query
如何把这个mysql指令转换成psql,这样用户的经纬度就变成了startlng
和startlat
?
不评论计算本身,这个 PostgreSQL 的“端口”应该可以工作:
select * from (select SQRT(
POW(69.1 * (10 - 8), 2) +
POW(69.1 * (100 - 10) * COS(10 / 57.3), 2)) AS distance
from table) d where distance < 25 ORDER BY distance;