Update the remainder of a table from the data contained in a row

Asked

Viewed 37 times

1

The figure below explains well what I want to try to do (and what I tried to do but it didn’t work):

inserir a descrição da imagem aqui

That is, I need to update some rows of a table with the same data contained in a row (specifically, the columns bankId, proj_day and liq_day, in red in the figure above). Note that the id column is equal to the entryid column of the other rows that need to be filled (in green in the figure above). The query I built didn’t work and there are hundreds of data like this that need to be updated this way.

Would anyone know to give me a light? Thank you!

  • The image is a good illustration, but include your sql code in text format

1 answer

0


I found it curious that it didn’t work, even though it was declared with two separate banks, in his question he did not make it clear whether he needs to do this by relating two tables in different databases or whether he could assemble with a self-relationship within the same table in just one database. So I took your example and set up a test table like below:

inserir a descrição da imagem aqui

I left with the default value null the fields that should be updated then built the querie:

 UPDATE flux AS A INNER JOIN flux AS B ON A.entryId = B.id 
 SET 
     A.proj_day = B.proj_day, 
     A.liq_day = B.liq_day, 
     A.bankId = B.bankId 
 WHERE A.userid = 307 AND A.entryID > 0    

I’ve built a relationship following her, the difference is that wanted above relates her to herself (self-relationship) across the countryside entryid and id.

Then surround the querie and works perfect as picture below:

inserir a descrição da imagem aqui

What I think is wrong is that it is using two different databases, possibly created a new database to not affect the data that are about production and to have a testing environment. Since you did not specify the error, in your question, it became difficult to help you more. Using the above relationship would work perfect!

  • You are so right! Actually the query was correct and I didn’t need to duplicate the same database to make the Internet (in fact, I forgot to mention this in the question). Besides, I was with the database view that does not receive the update. What a mess my... Thank you very much for the clarification!

Browser other questions tagged

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