简体   繁体   中英

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

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 ...

you can cast the sum result to 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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