尝试在 PostgreSQL 11 中执行 SELECT 查询时出现错误
select (
SELECT STRING_AGG(u.first_name::text, ', ')
FROM game_authors AS gat
LEFT JOIN users AS u ON u.id = gat.user_id
WHERE gat.game_id = g.id AND gat.lang = 'uk'
GROUP BY gat.id ORDER BY gat.id ASC
) AS authors_string
from "games" as "g"
where "g"."status" != 10
order by "g"."id" desc limit 10 offset 0
并且 authors_string 应该作为字符串值获取。它抛出了一个错误
错误:用作表达式的子查询返回多行
我猜它是因为子查询中的 GROUP BY 而发生的,并且可以使用 row_to_json 函数处理,但不知道我应该将它放在表达式中的哪个位置。ORDER BY 不起作用 GROUP BY 表达式,因为 SELECT 中存在聚合函数。
SQL 版本
x86_64-pc-linux-gnu 上的 PostgreSQL 11.8 (Ubuntu 11.8-1.pgdg18.04+1),由 gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0 编译,64 位