Update in two MYSQL tables

Asked

Viewed 765 times

-1

How to update two tables, e.g.: first table users, would update only on senha, but on the table users_info would update cos fields, cidade, nascimento, sexo and etc.

I can do it in one, but I wanted to do both in one operation, I read about INNER JOIN, but did not understand very well to update

UPDATE ?? SET ?? = ? WHERE ?? = ?

  • Join in the update syntax is not for your situation. In your case, two update commands must be executed, which you can do, is to place these two commands inside a transaction, and thus make sure that the two will be executed correctly.

  • This question has been answered in https://answall.com/questions/233300/update-inner-join-php-mysql/233321#233321

1 answer

0

You must make the INNER JOIN through the user ID, and make the changes you want. In the example below, I gave the nickname u to the table users, and i to the table users_info:

UPDATE users u INNER JOIN users_info i ON u.id = i.id 
SET u.senha = 'senha', i.cidade = 'cidade', i.nascimento = '1993-01-01', i.sexo = 'F';

Browser other questions tagged

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