我需要比较postgis上的一些空间数据,但我需要与另一个表“card_history”中的几何数据进行比较,包含使用“ST_GeomFromText”函数所需的lat和long字段。 但是如何在单个查询中执行此操作? 由于我正在研究php(女巫不相关),因此提出大量查询会非常痛苦......
我正在尝试这样的事情:
SELECT ST_Distance_Sphere
(ST_GeomFromText('POINT(
(SELECT DISTINCT ON (userid) long from card_history)
(SELECT DISTINCT ON (userid) lat from card_history))',4326),
ST_GeomFromText('POINT(17.0712215 48.15825)',4326)
)
但是子查询似乎不能作为“POINT”的参数。 请帮忙
答案 0 :(得分:3)
试试这个
WITH
lon AS (
SELECT DISTINCT ON (userid) AS long from card_history
),
lat AS (
SELECT DISTINCT ON (userid) AS lat from card_history
)
SELECT ST_Distance_Sphere(ST_GeomFromText(
'POINT(' || lon.lon || ' ' || lat.lat || ',4326'),
ST_GeomFromText('POINT(17.0712215 48.15825)',4326))
FROM lon, lat;