Home > Net >  How to mention inserted table in subselect postgres?
How to mention inserted table in subselect postgres?

Time:04-24

I need to insert some data in new column:

insert into orders (delivery_address) 
select address 
from addresses 
where addresses.id = orders.address_id;

I found that i cannot mention inserted table 'orders' in subquery. Is there a way to do that? Thanks.

CodePudding user response:

You can try to use EXISTS subquery with your condition.

insert into orders (delivery_address) 
SELECT address 
FROM addresses t1
WHERE EXISTS (
    SELECT 1
    FROM orders o
    WHERE t1.id = o.address_id
)
  • Related