2014-11-22 58 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"