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

Asked

Viewed 20,752 times

39

I know it’s possible to execute one 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’d like to do the same with a 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?

1 answer

46


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 on Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.