我对我在 PostgreSQL 中进行的查询有一个未解决的疑问。
我有这两张桌子
播放器
playerID title
1 Rondo
2 Allen
3 Pierce
4 Garnett
5 Perkins<
播放
playerID TeamID
1 1
1 2
1 3
2 1
2 3
3 1
3 3
这就是我的查询
SELECT DISTINCT concat(N.playerID, ':', N.title), TID
FROM player N
INNER JOIN (
SELECT DISTINCT P.playerID as PID, teamID as TID
FROM plays P
) AS derivedTable
ON N.playerID = PID
ORDER BY concat
查询的结果是:
"1:Rondo" | 1
"1:Rondo" | 2
"1:Rondo" | 3
"2:Allen" | 1
"2:Allen" | 3
"3:Pierce" | 1
"3:Pierce" | 3
但我想要这样的东西
"1:Rondo" | 1, 2, 3
"2:Allen" | 1, 3
"3:Pierce" | 1, 3
我可以使用 array_agg,但我真的不知道如何