3

我有 7 个 SQL 查询来完成这样的任务:

1. UPDATE Customer SET CustomerService = 'perta' WHERE FirstName = 'john';
2. UPDATE Customer SET Flag = 1 WHERE OrderNum BETWEEN 2 AND 29;
3. UPDATE Customer SET PurchaseNum = PurchaseNum + 60 WHERE OrderNum BETWEEN 2 AND 29;
4. UPDATE Customer SET OrderNum = OrderNum + 60 WHERE OrderNum BETWEEN 2 AND 29;
5. UPDATE Customer SET PurchaseNum = PurchaseNum - 28 WHERE (PurchaseNum > 29 AND PurchaseNum <= 89) AND (Flag <> 1);
6. UPDATE Customer SET OrderNum = OrderNum - 28 WHERE (OrderNum > 29 AND OrderNum <= 89) AND (Flag <> 1);
7. UPDATE Customer SET Flag = 0 WHERE OrderNum BETWEEN 62 AND 89;

是否可以将这些 SQL 查询“压缩”为 1 个查询?

因为我担心用户通过按 ESC 按钮(在他/她按 SUBMIT 按钮后)取消该过程,这些序列会在中间被破坏,我的桌子也会很乱。

4

1 回答 1

1

如果您使用的是 MySQL,请查看此链接,它向您展示了如何在 MySQL 中实现事务。

http://dev.mysql.com/doc/refman/5.5/en/commit.html

事务在许多情况下都可以帮助我们,并且您正在处理其中一个要更新多个数据库表的情况。

于 2012-04-11T10:13:18.413 回答