2

我有一个像下面这样的查询,我需要它在一个更新查询中。由于我是初学者,我使用连接并编写了单行查询,但出现错误

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

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

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';

下面是我尝试过的内容并得到了如上所述的错误...

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';
4

2 回答 2

1

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

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

您必须在 Mysql 命令行中执行此操作...

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

java的希望......这个链接可能有用 http://www.easywayserver.com/jdbc/JDBC-prepared-statement.htm

于 2012-04-05T11:42:19.950 回答
0

你必须使用交易

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;

您可以在此处阅读有关将锁表与事务组合的信息

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

于 2012-04-05T10:45:31.110 回答