我在两个表中选择电子邮件,如下所示:
select email
from table1
inner join table2 on table1.person_id = table2.id and table2.contact_id is null;
现在我在table2中有一个名为email
我想使用上面选择的电子邮件值更新table2的电子邮件列。
请告诉我使用POSTGRES的选择sql语法进行的更新
修改
我不想发布另一个问题。所以我在这里问:
上面的select语句返回多行。我真正想要的是:
update table2.email
if table2.contact_id is null with table1.email
where table1.person_id = table2.id
我不知道该怎么做。我上面的select语句似乎不正确。
请帮忙。
我可能已找到解决方案:
Update a column of a table with a column of another table in PostgreSQL
答案 0 :(得分:1)
你有没有试过像:
UPDATE table2 SET
email = (SELECT email
FROM table1
INNER JOIN table2 ON table1.person_id = table2.id AND table2.contact_id IS NULL)
WHERE ...
答案 1 :(得分:1)
UPDATE Table2
SET email =
(SELECT
email
FROM
table1
JOIN
table2
ON
table1.person_id = table2.id
WHERE
table2.contact_id is null) dt
WHERE
<<SOME CONDITION THAT ISOLATES WHICH ROWS YOU WANT TO UPDATE >>
答案 2 :(得分:1)
我一直在寻找以下解决方案。
Update a column of a table with a column of another table in PostgreSQL
UPDATE table2 t2
SET val2 = t1.val1
FROM table1 t1
WHERE t2.table2_id = t1.table2_id
AND t2.val2 IS DISTINCT FROM t1.val1 -- to avoid empty updates