首页 > 解决方案 > 使用 string_agg 更新 Postgresql

问题描述

我有两张表,分别是表 1 和表 2。我需要将两张表与派对 ID 匹配。如果当事人 id 匹配并且域也匹配,则无需更新无效电子邮件列中的任何内容。如果当事方 id 匹配并且域与表 1 不匹配,则在无效列中使用逗号分隔更新所有域

回覆

任何人都可以帮我解决这个问题......!

标签: postgresql

解决方案


你可以试试这个:

update table2
  set invalid_email=array_to_string(t1.domaines,';') 
  from (select party_id,array_agg(domaine) as domaines from table1 group by party_id) t1 
 where table2.party_id=t1.party_id and table2.domaine not in (select unnest(domaines))

推荐阅读