How to update fields with fields from another table?

Asked

Viewed 625 times

5

I’m not able to update one table with another’s select.

I have a table mdl_user which contains several columns and created another users_sg to copy some columns.

I copied with

INSERT INTO
  users_sg (id, username, firstname, lastname)
SELECT
  id, username, firstname, lastname FROM mdl_user

I wanted to keep updating, but I could only do one column and one user at a time:

UPDATE
  users_sg
SET
  users_sg.username = (SELECT username FROM mdl_user where id=3)
WHERE id=3

I have to update the fields username, firstname and lastname of all users at once?

  • The update will be periodic, or when it is changed in one table you want it to update automatically in the other?

  • @Celsomarigojr I think it’s for once.

  • @Jorgeb. but in this case the Insert would already solve!?

  • @Celsomarigojr because maybe, is a little confused. Fabio can explain?

  • The answer helped?

  • Hi, I will need to run the command at least once a day to update the ones that already exist.

Show 1 more comment

1 answer

6


You can make a UPDATE WITH JOIN with the two tables to update the 3 columns, user or Where if it is a single record that has to be changed.

UPDATE users_sg as sg 
join mdl_user as user_ on user_.Id = sg.Id
SET sg.username = user_.username,
sg.firstname = user_.firstname,
sg.lastname = user_.lastname;
-- where user_.Id = 3 -- caso seja só de um unico registro mais com os 3 campos.

To do this you will automatically need to use an event.

DELIMITER $
CREATE EVENT AtualizaTabela 
    ON SCHEDULE EVERY 1 DAY
    STARTS NOW()
    DO BEGIN 
            UPDATE users_sg as sg 
            join mdl_user as user_ on user_.Id = sg.Id
            SET sg.username = user_.username,
            sg.firstname = user_.firstname,
            sg.lastname = user_.lastname;
    END
$ DELIMITER 
  • In this Goku example I would be able to update only 1 user at a time, correct? I would need to update them all at once.

  • Without Where you will update all Where the ids are equal

  • I understood Goku, that would be right, but I tried running this command now and it gives the following error: #1064 - You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near 'from users_sg Sg Join mdl_user user_ on user_.id = Sg.id' at line 5

  • Semicolon at the end of commands

  • Hi, I put the dot and comma, it looked like this: UPDATE Sg SET Sg.username = user_.username, Sg.firstname = user_.firstname, Sg.lastname = user_.lastname; from users_sg Sg; Join mdl_user user_; on user_.Id = Sg. Id; But the error was: #1146 - Table 'basetest.Sg' doesn’t exist; Does it try to create another table? Wasn’t supposed to update users_sg?

  • Look at the edition, you put ; within the command the ; has to be at the end of each type command select * from tabela ;

  • Goku, I’m sorry, but I don’t understand, I circled in phpmyadmin like this: UPDATE sg
SET sg.username = user_.username,
sg.firstname = user_.firstname,
sg.lastname = user_.lastname;
from users_sg sg;
join mdl_user user_;
on user_.id = sg.id; Note that I put ; at the end of each instruction (SET, from, Join, on), which I did wrong?

  • No, the point and comma has to be at the end of everything.. on user_.id = Sg.id; <-- here.

  • Oh yes, I had done that too. But the error is: #1064 - You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near 'from users_sg Sg Join mdl_user user_ on user_.id = Sg.id' at line 5

  • Vacilo my, mysql is different the syntax. I edited the question

  • 1

    Goku, no words, thank you very much...

Show 6 more comments

Browser other questions tagged

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