Update multiple tables in a single query in mysql

北城以北 提交于 2019-12-24 03:20:03

问题


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;


来源:https://stackoverflow.com/questions/39139831/update-multiple-tables-in-a-single-query-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!