我使用的是Postgresql 11。我有两个表:
Report_template
<表类>
tbody><<tr>1 1、2、3 23、2、1 3 2, 2, 3, 1 43, 1 表类>
id
(int)template_blocks_id
(int [])您需要连接到unnest的结果,以便重复行:
select rt.id as report_template_id,
string_agg(rtb.code, ',' order by b.idx) as aggregated_code
from report_template rt
cross join unnest(rt.template_blocks_id) with ordinality as b(template_id, idx)
join report_template_block rtb on rtb.id = b.template_id
group by rt.id
order by rt.id;