首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在MySQL中同时更新多个表

问在MySQL中同时更新多个表
EN

Stack Overflow用户
提问于 2012-04-05 10:32:12
回答 2查看 452关注 0票数 2

下面有一个类似于这些的查询,我需要它在一个更新查询中。由于我是初学者,我使用联接并编写了单行查询,但我得到的错误是

超过锁定等待超时;尝试重新启动事务

下面是每个单独的更新查询..。

代码语言:javascript
复制
update a set id=id-1 where id>'3' and reg='34554';
update b set id=id-1 where id>'3' and reg='34554';
update c set id=id-1 where id>'3' and reg='34554';
update d set id=id-1 where id>'3' and reg='34554';
update e set id=id-1 where id>'3' and reg='34554';

下面是我试过的错误,正如上面所说的.

代码语言:javascript
复制
update a
LEFT JOIN b ON b.id=a.id and b.tan=a.tan
LEFT JOIN c ON c.id=b.id and c.tan=b.tan
LEFT JOIN d ON d.id=c.id and d.tan=c.tan
LEFT JOIN e ON e.id=d.id and e.tan=d.tan
SET a.id=b.id=c.id=d.id=e.id=a.id-1
where a.id>'3' and a.tan='34554';
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-05 11:42:19

您可以在存储过程中存储多个sql,然后从java..Id和reg可以通过参数传递。

关于存储过程的信息http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html

你必须在Mysql命令行中这样做..。

代码语言:javascript
复制
Delimiter |

Create procedure (in id_val int, in reg_val int)

Begin

update a set id=id-1 where id>id_val and reg=reg_val;

update b set id=id-1 where id>id_val and reg=reg_val;

update c set id=id-1 where id>id_val and reg=reg_val;

update d set id=id-1 where id>id_val and reg=reg_val;

update e set id=id-1 where id>id_val and reg=reg_val;

End |

delimiter ;

我用PHP..。以下链接http://php.net/manual/en/pdo.prepared-statements.php中的信息

爪哇的希望..。此链接可能是有用的http://www.easywayserver.com/jdbc/JDBC-prepared-statement.htm。

票数 1
EN

Stack Overflow用户

发布于 2012-04-05 10:45:31

你必须使用交易

代码语言:javascript
复制
SET autocommit=0;
LOCK TABLES a WRITE, b WRITE, c WRITE, d WRITE, e WRITE

update a set id=id-1 where id>'3' and reg='34554';
update b set id=id-1 where id>'3' and reg='34554';
update c set id=id-1 where id>'3' and reg='34554';
update d set id=id-1 where id>'3' and reg='34554';
update e set id=id-1 where id>'3' and reg='34554';

COMMIT;
UNLOCK TABLES;

您可以阅读有关将锁表与事务这里组合的文章。

我不知道,如何用join创建这样的查询。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10026939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档