Insert data from one table into another comparing ID

0

In my system I have two tables, one with user registries, of course, each one has its ID.

And another table where the sending data is placed from one user to another, but only the IDs of the sender and receiver are shown.

I added two more columns in the gift table that would be for the names and username of the users who received the gift, so I knew who was given by name and not by ID.

My idea would be to update / insert the names and usernames by comparing the ID of the recipient with the ID of the user table.

Just to state, I bought the system ready and wanted to implement, I tried anyway to replace so that the names were already for table, but nothing done.

    
asked by anonymous 21.01.2018 / 21:13

1 answer

0

Your concept is wrong, you do not need to duplicate data in your database. For you to retrieve the name of the user who gave the gift just make a INNER JOIN , for example:

SELECT a.id, userDoador.nome AS nome_doador, userReceptor AS nome_receptor FROM Presentes a
INNER JOIN Usuarios userDoador ON a.id_user_doador = userDoador.id
INNER JOIN Usuarios userReceptor ON a.id_user_receptor = userReceptor.id;

In this way you do not need the additional fields in the Presentes , break table, so updating the user name will automatically affect the result of this SELECT .

See more about INNER JOIN

    
21.01.2018 / 21:27