Tornike
Tornike

Reputation: 235

Postgresql Select rows and update column

I have SQL Select query with where clauses. For e.g

select * from table where status = 1

And how can I update single column with selected rows simultaneously while selecting? I want to mark selected rows, to avoid reselect on the next loop. Something like:

select * from table where status = 1; update table set proc = 1 where id in (select id from table where status = 1)

But this query will not return results.

Upvotes: 3

Views: 1900

Answers (1)

user330315
user330315

Reputation:

Use the returning clause:

update table 
    set proc = 1 
where id in (select id from table where status = 1)
returning *;

(Btw: I assume the inner select is not actually selecting from the same table, because then the statement does not really makes sense as it could be rewritten with a simple where stauts = 1)

Upvotes: 3

Related Questions