Update multiple tables in a single query in mysql

2019-07-12 19:15发布

问题:

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;

回答1:

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;