2014-11-22 34 views
0
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,计算的字段。如何圆一个计算字段在MySQL查询

+0

'由'组'? – 2014-11-22 15:41:47

+0

我要改变:) – 2014-11-22 16:00:44

回答

1

使用如前所述here所以,你的查询将ROUND函数:

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