sql >> Base de Datos >  >> RDS >> PostgreSQL

Actualizar o insertar (múltiples filas y columnas) desde la subconsulta en PostgreSQL

Para la ACTUALIZACIÓN

Usar:

UPDATE table1 
   SET col1 = othertable.col2,
       col2 = othertable.col3 
  FROM othertable 
 WHERE othertable.col1 = 123;

Para el INSERTO

Usar:

INSERT INTO table1 (col1, col2) 
SELECT col1, col2 
  FROM othertable

No necesitas los VALUES sintaxis si está utilizando SELECT para completar los valores INSERT.