Update from comparison between two postgres tables

Asked

Viewed 1,193 times

2

I have a sales chart that contains the seller’s code.
However the seller’s table was all redone, with the change of id.

How can I update the seller code in the sales table from the comparison between the new seller table and the old?

I tried to make join but it was done update with the same id from seller to all table records vendas.

  • The first thing you have to do is define how to know that two records, one from the old table and the other from the new table, refer to the same seller. Having defined this match is only update the sales table, for example with the result of joining the new and old seller tables. From what you describe the result most likely your WHERE clause is wrong.

  • Assuming that the seller table id is the primary key of this table and that such a field was used as a foreign key in the sales table, how do you think you can associate the old id with the new id? What are the definitions of your tables and particularly the definition of foreign keys.

  • What is it possible to compare in the tables to define the association between the old id and the new one generated when you remade the seller table? I hope you have a backup of your bank in the position prior to the action of redoing the table.

1 answer

0

To be used JOIN you must have a relationship between the tables, otherwise you won’t be able to, see how it can be done:

UPDATE Vendas set camposVendas = novoValor,... JOIN TabelaVendedor1 as TB1 ON TB1.campoVerifica = campoTabelaVenda ou TB2 JOIN TabelaVendedor2 as TB2 ON TB2.campoVerificad = campoTabelaVenda ou TB1

Remember, there must be a relationship between these tables for this instruction to work. I hope I helped!

  • The way to do a JOIN in a command UPDATE in Postgresql is to use the clause FROM and not the clause JOIN. See the documentation.

Browser other questions tagged

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