Update with Inner Join

Asked

Viewed 36,430 times

14

If I make a consult like update using the clause Inner Join, the update will affect all fields that satisfy the condition imposed on Inner Join or just the first field?

  • 2

    You can put an example query to clarify the question?

  • You can find a solution here

2 answers

31


The UPDATE will only be done in the column of the table you are designating, in other words, only the columns with SET

Take the example:

UPDATE tb1
SET tb1.column_1 = tb2.column_1
FROM table_1 AS tb1
INNER JOIN table_2 AS tb2
ON tb1.column_2 = tb2.column_3

Now, to answer your question, the clause ON serves to filter the records you want to update, ie looking at the example, only the columns that meet the equivalence will be updated ON tb1.column_2 = tb2.column_3.

I hope I’ve helped.

2

Just complementing the answer already well explained by Rafael Gomes, it may seem obvious, but I think it is worth noting that besides being updated only the column defined in the clause SET (SET tb1.column_1 = tb2.column_1), ie in this case only tb1.column_1, will be updated ALL records (rows) of this column satisfying the condition (ON tb1.column_2 = tb2.column_3).

Browser other questions tagged

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