在mysql中的1个事务中有3个查询

时间:2011-12-20 13:17:36

标签: mysql

我有这个查询,首先检查项目是否仍然可用。如果它仍然可用,那么服务器将返回该项然后自动递减它..这是我的语法,我想要出现的是我只需要请求一次所以我需要将这3个查询加入一个。可能吗?我能问一个样品吗?

IF SELECT EXISTS (SELECT * FROM inventorylist WHERE BarcodeNumber = '9555440808279' AND NotificationQuantity > 0)
BEGIN  
   UPDATE inventorylist SET NotificationQuantity = NotificationQuantity - 1 WHERE BarcodeNumber = '9555440808279';
   SELECT * FROM inventorylist WHERE BarcodeNumber = '9555440808279';
END

此语法将返回错误..

[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF SELECT EXISTS (SELECT * FROM inventorylist WHERE BarcodeNumber = '95554408082' at line 1
[Err] IF SELECT EXISTS (SELECT * FROM inventorylist WHERE BarcodeNumber = '9555440808279' AND NotificationQuantity > 0)
BEGIN  
UPDATE inventorylist SET NotificationQuantity = NotificationQuantity - 1 WHERE BarcodeNumber = '9555440808279';

2 个答案:

答案 0 :(得分:2)

怎么样?
START TRANSACTION;
SELECT @s:=(SELECT ifnull(count(*),0) FROM inventorylist WHERE BarcodeNumber='9555440808279' and NotificationQuantity>0);
UPDATE inventorylist SET NotificationQuantity=NotificationQuantity-@s WHERE BarcodeNumber='9555440808279' and NotificationQuantity>0);
SELECT  inventorylist.*,@s AS success FROM inventorylist;
COMMIT;

这将始终回馈您的文章并添加成功字段。

答案 1 :(得分:0)

首先,如果你只关心它的EXISTS那么就不要打扰“SELECT *”了。只需返回一个字段:

IF SELECT EXISTS (SELECT BarcodeNumber FROM inventorylist WHERE BarcodeNumber = '9555440808279' AND NotificationQuantity > 0)

确实没有办法(我知道)用UPDATE syntax返回UPDATEd行。所以我认为你最好的办法就是将你已经存在的内容放入存储过程中。