简体   繁体   English

如果数字是整数,MySQL 从小数部分删除零

[英]MySQL remove zeros from decimal part if the number is whole

I have a sales table with price column as decimal(8, 2) and I'm running the query below to get the total price我有一个销售表, price列为decimal(8, 2) ,我正在运行下面的查询以获取总价

select sum(case when status = 1 then price * quantity when status = 2 then price * quantity * -1 else 0 end) as total from sales;

It's working perfectly but my results are usually intergers so I end up with trailing zeros all the time.它工作得很好,但我的结果通常是整数,所以我总是以尾随零结束。 It'd be nice to just return the whole number if there is no decimal part如果没有小数部分,最好只返回整数

like akina said in his comment ...就像akina在他的评论中所说的那样......

you can cast the sum result to UNSIGNED INTEGER您可以总和结果转换为 UNSIGNED INTEGER

select 
CAST(sum(case when status = 1 then price * quantity when status = 2 then price * quantity * -1 else 0 end) AS UNSIGNED)
 as total from sales;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM