Home > Software design >  How to update values in two tables where one is usermeta MySQL
How to update values in two tables where one is usermeta MySQL

Time:07-16

I have two tables and I need to make single SQL request which will update values in both of them.

Their releation is based on ID (1 table = evdb_users.ID / 2 table = evdb_usermeta.user_id)

For now I did some SQL query like this:

"UPDATE evdb_users
       INNER JOIN evdb_usermeta ON evdb_users.ID = evdb_usermeta.user_id AND evdb_usermeta.meta_key='phone_number'
       SET evdb_users.user_login='%s', evdb_users.user_email='%s', evdb_users.display_name='%s', evdb_usermeta.meta_value='%s'
       WHERE evdb_users.ID=%d"

Data in evdb_users (login/email/display name) - are updating, but nothing changes in usermeta (phone_number)

Please help me to figure out how it should be and how it must works.

CodePudding user response:

try this:

UPDATE evdb_users
       INNER JOIN evdb_usermeta
         ON  evdb_users.ID = evdb_usermeta.user_id 
         AND evdb_usermeta.meta_key='phone_number'
       SET evdb_users.user_login='%s'
         , evdb_users.user_email='%s' 
         , evdb_users.display_name='%s' 
         , evdb_usermeta.meta_value='%s'
       WHERE evdb_users.ID=%d;
  • Related