Francesco G.
Francesco G.

Reputation: 717

Update multiple tables in a single query in mysql

I have three query and I would like to have a single one. These is my query:

UPDATE tab1 SET a='' WHERE id=3;
UPDATE tab2 SET b='' WHERE id=9;
UPDATE tab3 SET c='' WHERE id=5;

Upvotes: 2

Views: 9451

Answers (1)

Vikrant
Vikrant

Reputation: 5046

You can try below code:

UPDATE tab1, tab2, tab3
SET tab1.a = '', tab2.b = '',tab3.c = ''
WHERE tab1.id = 3 AND tab2.id = 9 AND tab3.id = 5;

UPDATE:

As per mentioned by OP, the code not working for Mysql 5.5, below code added

UPDATE tab1 a 
  INNER JOIN tab2 b ON (a.id = b.id)
  INNER JOIN tab3 c ON (a.id = c.id)
SET tab1.a = '', tab2.b = '', tab3.c = ''
WHERE a.id = 3 AND tab2.id = 9 AND tab3.id = 5;

Upvotes: 3

Related Questions