MySQL取小数点后两位及百分比

论坛 期权论坛 脚本     
匿名技术用户   2021-1-3 14:17   87   0
1、ROUND可以进行四舍五入,按照对应的位数

2、TRUNCATE直接按照位数截取,不四舍五入
实例:
SELECT ROUND(RAND(),4),TRUNCATE(RAND(),4);
SELECT c.c_name,COUNT(*) saleCount,SUM(a.item_sale_price) itemSumPrice,SUM(a.item_sale_price)-SUM(b.item_base_price) grossProfit,
CONCAT(ROUND((SUM(a.item_sale_price)-SUM(b.item_base_price))/SUM(a.item_sale_price)*100,2),'%') grossProfitMargin,
COUNT(DISTINCT a.order_id) saleSumPeople,
TRUNCATE(IFNULL(SUM(a.item_price),0)/COUNT(DISTINCT a.order_id),2) perUnitPrice
FROM auge_sale a
LEFT JOIN auge_item b ON b.item_barcode = a.item_barcode
INNER JOIN auge_item_classification c ON c.c_id=b.item_cid
GROUP BY c.c_name
ORDER BY itemSumPrice DESC

花费了近一个多小时的时间把sql语句完成,查询品类的各项参数的统计。不过有两点不足:

1、sql不利于后期的维护,前端页面可以直接拿,但是后期修改比较麻烦。

2、这样写下来代码的模块化不强,实现基本上在最底层的sql原生语句

转载于:https://my.oschina.net/inchlifc/blog/1576495

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP