在mysql中将正值更新为负值

时间:2012-06-27 12:52:22

标签: mysql

我有付款表字段

enter image description here

更新原因和金额&总字段是负变量

UPDATE payment 
SET reason = 'refund' 
WHERE uid =5 AND date = '2012-05-01' AND accid =2 

更新单个查询是否可能?

3 个答案:

答案 0 :(得分:15)

如果我理解正确,您还需要将amount列设置为正值以及上述语句。

你可以使用这样的东西

UPDATE payment 
SET reason = 'refund', amount = amount * -1, total = total * -1
WHERE uid =5 AND date = '2012-05-01' AND accid =2

答案 1 :(得分:4)

如果您希望始终获得正整数,请使用ABS(amount)

SELECT ABS(5);

将输出5

SELECT ABS(-5);  

也将输出5

答案 2 :(得分:0)

当我寻找解决方案时,所提供的建议破坏了我的结果:

SELECT @TotalAmount:=( SELECT FORMAT(SUM(Amount),   4)  FROM MPPayment WHERE PaymentBatchID = 6 and CompanyID=3);

正确的结果:

enter image description here

格式化后:

SELECT @TotalAmount:=( SELECT FORMAT(SUM(Amount),   4)  FROM MPPayment WHERE PaymentBatchID = 6 and CompanyID=3);
SELECT @TotalAmount * -1;

enter image description here 可能不适用于格式化。

另一种解决方案是从零减去您的数字:

SELECT @TotalAmount:=( SELECT SUM(Amount)  FROM MPPayment WHERE PaymentBatchID = 6 and CompanyID=3);
 select FORMAT((0 - @TotalAmount), 4 ); 

为避免损坏上述结果,我在操作结束时进行了格式化。结果很好:

enter image description here

也可以乘以-1:

 SELECT @TotalAmount:=( SELECT SUM(Amount)  FROM MPPayment WHERE PaymentBatchID = 6 and CompanyID=3);
 select FORMAT(( @TotalAmount *-1), 4 );