如何在mysql查询中舍入计算字段

时间:2014-11-22 15:37:50

标签: mysql

select *, sum(price+shipping+paypalfee+storefee) as totalcost, customerpaid as totalrevenue, 
(customerpaid - sum(price+shipping+paypalfee+storefee)) as profit,
(((customerpaid - sum(price+shipping+paypalfee+storefee)) / customerpaid) * 100.00) as profitpercent
from tblsales 
group by orderno having " . $having . " 
order by $sort $order limit $offset,$rows"

查询工作正常我怎样才能圆润profitpercent ,一个计算字段。

2 个答案:

答案 0 :(得分:1)

使用上面提到的ROUND函数here所以你的查询将是:

SELECT orderno, sum(price+shipping+paypalfee+storefee) as totalcost, customerpaid AS totalrevenue, (customerpaid - sum(price+shipping+paypalfee+storefee)) AS profit, ROUND((((customerpaid - sum(price+shipping+paypalfee+storefee)) / customerpaid) * 100.00)) AS profitpercent
FROM tblsales 
GROUP BY orderno HAVING " . $having . " 
ORDER BY $sort $order LIMIT $offset,$rows"

答案 1 :(得分:0)

一般

ROUND( expression, 2 )

所以试试这个

select *, sum(price+shipping+paypalfee+storefee) as totalcost, customerpaid as totalrevenue, 
(customerpaid - sum(price+shipping+paypalfee+storefee)) as profit,
ROUND((((customerpaid - sum(price+shipping+paypalfee+storefee)) / customerpaid) * 100.00),2) as profitpercent
from tblsales 
group by orderno having " . $having . " 
order by $sort $order limit $offset,$rows"