将7个这样的SQL查询转换为单个查询,是否可能?

时间:2012-04-11 09:38:29

标签: mysql sql

我有7个SQL Query来执行这样的任务:

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按钮之后),这些序列将在中间被打破,我的桌子也会变得混乱。

1 个答案:

答案 0 :(得分:1)

如果您使用的是MySQL,那么请看这个链接,它会告诉您如何在MySQL中实现事务。

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

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