MySQL 在mysql中将正值更新为负值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11226644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
update positive to negative value in mysql
提问by Vaishu
i have payment table fields
我有付款表字段
update reason and amount & total field are change negative
更新原因和金额和总计字段更改为负
UPDATE payment
SET reason = 'refund'
WHERE uid =5 AND date = '2012-05-01' AND accid =2
update single query is it possible?
更新单个查询是否可能?
回答by Fahim Parkar
If I understand you correctly, you also want to set amount column to positive value along with the above statement.
如果我理解正确,您还想将金额列与上述声明一起设置为正值。
You can use something like this
你可以使用这样的东西
UPDATE payment
SET reason = 'refund', amount = amount * -1, total = total * -1
WHERE uid =5 AND date = '2012-05-01' AND accid =2
回答by Andrew Atkinson
Use ABS(amount)
if you wish to always get the positive integer.
使用ABS(amount)
,如果你想始终获得正整数。
SELECT ABS(5);
will output 5
将输出 5
SELECT ABS(-5);
will also output 5
也会输出 5
回答by Mr.B
When I looked for the solution, the offered suggestion corrupted my result:
当我寻找解决方案时,提供的建议破坏了我的结果:
SELECT @TotalAmount:=( SELECT FORMAT(SUM(Amount), 4) FROM MPPayment WHERE PaymentBatchID = 6 and CompanyID=3);
Proper result:
正确结果:
After formatting:
格式化后:
SELECT @TotalAmount:=( SELECT FORMAT(SUM(Amount), 4) FROM MPPayment WHERE PaymentBatchID = 6 and CompanyID=3);
SELECT @TotalAmount * -1;
Probably doesn't work well with formatting.
Another solution is to subtract your digit from zero:
另一种解决方案是从零中减去您的数字:
SELECT @TotalAmount:=( SELECT SUM(Amount) FROM MPPayment WHERE PaymentBatchID = 6 and CompanyID=3);
select FORMAT((0 - @TotalAmount), 4 );
To avoid the corruption of the the result I described above, I make formatting at the end of the operation. The result is fine then:
为了避免我上面描述的结果被破坏,我在操作结束时进行格式化。结果很好:
Works also with multiplication by -1:
也适用于乘以 -1:
SELECT @TotalAmount:=( SELECT SUM(Amount) FROM MPPayment WHERE PaymentBatchID = 6 and CompanyID=3);
select FORMAT(( @TotalAmount *-1), 4 );