Mysql查询更新行并使用相同的查询返回新值

时间:2014-02-28 11:28:28

标签: php mysql sql-update

我有这个简单的查询来更新产品数量:

$updprqty='UPDATE products set     
available_qty = available_qty - '.$qty.' 
WHERE id = '.$pid.'';    

我还应该添加什么,以便获得available_qty的新值? 感谢

2 个答案:

答案 0 :(得分:1)

您也可以查看此链接,您可能会对此有更多的了解

Get Updated Value in MySQL instead of affected rows

答案 1 :(得分:0)

$updprqty='UPDATE products set     
available_qty = (select available_qty from products where id = ' .$pid . ') - '.$qty.' 
WHERE id = '.$pid.'';  

介绍别名

$updprqty='update products set available_qty =  x.available_qty from (select available_qty, id from products) - '.$qty.' where id = ' .$pid . ')  as x where id = x.id';