Is it possible to do an UPDATE with data from another table?

34

I know that it is possible to execute a INSERT with data from another table:

INSERT INTO Tabela (Col01, Col02, Col03)
    SELECT Col01, Col02, Col03
    FROM Outra_tabela
    WHERE Condicao = 'qualquer coisa'

But I would like to do the same with UPDATE . Something like this:

UPDATE Tabela SET Col01, Col02, Col03
    SELECT Col01, Col02, Col03
    FROM Outra_tabela
    WHERE Condicao = 'qualquer coisa'
WHERE Tabela.id = Outra_tabela.id

Is it possible? How?

    
asked by Gabe 08.12.2016 в 22:03
source

1 answer

38

It is possible to make a Inner Join

UPDATE Tabela
SET
  Tabela.Col01 = Outra_tabela.Col01,
  Tabela.Col02 = Outra_tabela.Col02
FROM
  Tabela
INNER JOIN Outra_tabela
  ON Tabela.id = Outra_tabela.id
WHERE
  Condicao = 'Qualquer coisa'

I put the code in GitHub for future reference.

    
answered by 08.12.2016 / 22:07
source