2011-04-06 63 views
0

嘿傢伙,我得到了一個大的數據庫表報告。
我想要的方式,從數額分割到40%和60%的計算,除(pro_tan)我希望他們是100%沒有拆分。php計算錶行

SHORT REPORT table: 
-------------- 
id t_deep t_tanning amount cashier 
----------------------------------------- 
1 deep pro tan  30  1 
2 facial tanning  25  2 
3 deep pro tan  30  1 
4 deep tanning  25  1 

report html result: 

table id cashier(40%) owns(60%) total 
1   12   18   30 
2   10   15   25 
3   12   18   30 
4   10   15   25 

預期結果:

table id cashier  owns  total 
1   0   30   30 
2   10   15   25 
3   0   30   30 
4   10   15   25 

PHP計算拆分爲40%和60%:

$q = $db->query("SELECT cashier, SUM(r_amount) AS amount, SUM(r_amount*0.60) AS totalSixty, SUM(r_amount*0.40) AS totalFourty FROM report 
     LEFT JOIN cashier ON cashier.id_cashier = report.id_cashier 
     WHERE date='$today' GROUP BY id_cashier"); 
// query to search pro tan 
$q1 = $db->query("SELECT cashier, SUM(r_amount) AS amount, SUM(r_amount*0.60) AS totalSixty, SUM(r_amount*0.40) AS totalFourty FROM report 
     LEFT JOIN cashier ON cashier.id_cashier = report.id_cashier 
     WHERE date='$today' AND t_tanning LIKE 'Pro Tan%' GROUP BY id_cashier"); 

$sixtyPercent = number_format($r['totalSixty'],2); 
$fourtyPercent = number_format($r['totalFourty'],2); 
+2

你的問題是什麼?它是「這個工作會嗎?」如果是這樣,是嗎? – MJB 2011-04-06 17:22:10

回答

1

你可以做這樣一個SUM(IF(t_tanning = 'pro tan',amount*0.6, amount))條件求和做1個查詢。