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 anonymous 08.12.2016 / 22:03

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.

    
08.12.2016 / 22:07