我有2个表,位置和邮政编码。
位置和zipcodes表都有一个点几何。
我写了一个查询,我可以为lng,lat传递字符串值,但是我想通过传递一个zipcode来做同样的事情,这就是距离结果的基础。
SELECT *, ST_distance(geometry::geography, ST_PointFromText('POINT(-118.0754445 32.4836479)', 4326)::geography) as dist
FROM locations
WHERE st_dwithin(geometry, ST_PointFromText('POINT(-118.0754445 32.4836479)', 4326)::geography, 1609)
ORDER BY dist;
答案 0 :(得分:0)
将两个表放在FROM子句中(为它们指定别名)并使用您需要的zipcode添加where条件:
SELECT *, ST_distance(l.geometry::geography, zc.geometry::geography ) as dist
FROM locations AS l, zipcodes AS zc
WHERE st_dwithin(l.geometry, zc.geometry::geography), 1609) AND
zc.code = 11111
ORDER BY dist;