Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgresql update column from select

Tags:

sql

postgresql

I am attempting to update a column from one table with a select query.

It runs and updates the entire type_ column as religious (text field).

I am trying to only update the rows where the religious geometry intersects the parcel geometry.

update wash_parcels_final
set    type_ = t.religious 
from   (select wash_worship.religious 
        from   wash_parcels_final 
        join   wash_worship 
        on     st_intersects(wash_worship.geom, wash_parcels_final.geom)) t
like image 370
ziggy Avatar asked Jun 03 '17 16:06

ziggy


People also ask

What is select for UPDATE Postgres?

The select ... for update acquires a ROW SHARE LOCK on a table. This lock conflicts with the EXCLUSIVE lock needed for an update statement, and prevents any changes that could happen concurrently. All the locks will be released when the transaction ends.

How does select for UPDATE work?

The SELECT FOR UPDATE statement is used to order transactions by controlling concurrent access to one or more rows of a table. It works by locking the rows returned by a selection query, such that other transactions trying to access those rows are forced to wait for the transaction that locked the rows to finish.

How UPDATE works in Postgres?

PostgreSQL implements multiversioning by keeping the old version of the table row in the table – an UPDATE adds a new row version (“tuple”) of the row and marks the old version as invalid. In many respects, an UPDATE in PostgreSQL is not much different from a DELETE followed by an INSERT .


1 Answers

I think this is what you want:

update wash_parcels_final
    set type_ = ww.religious 
    from wash_worship ww  
    where st_intersects(ww.geom, wash_parcels_final.geom);
like image 194
Gordon Linoff Avatar answered Nov 03 '22 09:11

Gordon Linoff