Postgresql选择行并更新列
我有带where子句的SQL Select查询。例如Postgresql选择行并更新列,sql,postgresql,select,sql-update,Sql,Postgresql,Select,Sql Update,我有带where子句的SQL Select查询。例如 select * from table where status = 1 在选择时,如何用选定的行同时更新单个列?我想标记所选行,以避免在下一个循环中重新选择。比如: select * from table where status = 1; update table set proc = 1 where id in (select id from table where status = 1) 但此查询不会返回结果。使用returnin
select * from table where status = 1
在选择时,如何用选定的行同时更新单个列?我想标记所选行,以避免在下一个循环中重新选择。比如:
select * from table where status = 1; update table set proc = 1 where id in (select id from table where status = 1)
但此查询不会返回结果。使用returning子句:
update table
set proc = 1
where id in (select id from table where status = 1)
returning *;
(顺便说一句:我假设内部select实际上不是从同一个表中进行选择,因为该语句实际上没有意义,因为它可以用一个简单的
重写,其中status=1
)只涉及一个表?顺便说一句:不要给你的表格命名table
;发明一些其他的元语法名称。