选择返回多个ID时,此时更新多行

时间:2018-01-23 09:42:26

标签: sql postgresql sql-update

我想根据PostgreSQL

中的以下查询更新表格
UPDATE tableX
SET columnX = 100
WHERE id = (SELECT ID FROM tableY WHERE col1 = 'Y' AND col2 = 'Z');

问题是:

  

选择查询可以返回多行

。如何仅使用一个查询更新表?

3 个答案:

答案 0 :(得分:3)

使用IN代替=

update tableX
set columnX = 100
where id IN (select id from tableY where col1 = 'Y' and col2 = 'Z');

答案 1 :(得分:2)

您可以检查ID是否在返回的列表中。

update tableX
set columnX = 100
where id in (select id from tableY where col1 = 'Y' and col2 = 'Z');

答案 2 :(得分:2)

为了完整性:

您首先不需要使用子查询。你也可以使用:

update tableX
set columnX = 100
from tableY
where tableX.id=tableY.id
and tableY.col1 = 'Y'
and tableY.col2 = 'Z';
相关问题