gpt4 book ai didi

Postgresql如何根据主选择中的值在选择中进行子选择

转载 作者:行者123 更新时间:2023-11-29 12:49:15 25 4
gpt4 key购买 nike

我正在尝试根据在主选择中找到的位置选择 5 个最近的地方。到目前为止,我已经做到了这一点。

SELECT p.*, 
ST_AsGeoJSON(p.coordinate) as coordiantes,
(
SELECT json_agg(op.id) AS otherplaces
FROM places op
GROUP BY op.coordinate, op.id
ORDER BY coordinate <-> st_geogfromwkb(p.coordinate)
LIMIT 5
) AS other
FROM places p
WHERE id = 1

如您所见,我希望调用 other 值,其中将包含 (id,name) 子选择结果的数组。

但是现在我得到一个错误。 "op.coordiante"必须出现在group by 中。当我添加该表达式时,我收到其他错误。 “在子选择中更多的是单行返回”。

那么如何做正确呢?

谢谢

最佳答案

SELECT p.*, 
ST_AsGeoJSON(p.coordinate) as coordiantes,
(
SELECT json_agg(foo.id) AS otherplaces
FROM
(
SELECT op.id
FROM places op
ORDER BY coordinate <-> st_geogfromwkb(p.coordinate)
LIMIT 5
) AS foo
) AS other
FROM places p
WHERE id = 1

关于Postgresql如何根据主选择中的值在选择中进行子选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58143316/

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