我去了一张桌子matches
。对于彼此对战的两个团队(name
和uuid
),匹配都有team_a_uuid
和team_b_uuid
。
uuid | name | team_a_uuid | team_b_uuid
-----|------|-------------|------------
d7e..| foo | 5db46a15-...| 82732895-..
334..| bar | 75ab1a39-...| 9fcedf80-..
... | ... | ... | ...
我有第二张桌子teams
。
uuid | player_1_uuid | player_2_uuid
-----|---------------|--------------
729..| f432f7bc-63...| e022ccb6-7...
d0f..| c9548a8e-b7...| a28441cb-2...
... | ... | ...
以下是我想要的内容:给我一个团队以及它所扮演的匹配游戏
uuid | player_1_uuid | player_2_uuid | match_uuids
-----|---------------|---------------|---------------------
729..| f432f7bc-63...| e022ccb6-7... | {'d7e...', '334...'}
d0f..| c9548a8e-b7...| a28441cb-2... | {'abc...', 'def...'}
我真的被困在这里了。干杯!
答案 0 :(得分:3)
SELECT t.uuid, t.player_1_uuid, t.player_2_uuid, array_agg(m.uuid) as "match_uuids"
FROM teams t JOIN matches m ON t.uuid IN (m.team_a_uuid, m.team_b_uuid)
GROUP BY t.uuid;
答案 1 :(得分:1)
关键是array_agg()
。您可以使用显式join
或相关子查询来执行此操作:
select t.*,
(select array_agg(m.uuid)
from matches m
where t.uuid in (m.team_a_uuid, m.team_b_uuid)
) as match_uuids
from teams t;