如何仅从特定表类型计算总计,我有表:

NAME   AMOUNT   TYPE

John   $15      cash

Dan    $15      check

Ken    $15      check

Karen  $20      cash

ron    $40      credit

Total   $105

cash     $35

check    $30

credit   $40

$totalPayments = 0;
$totalCash = 0;
$totalCheck = 0;
$totalCredit = 0;


while($pRow = mysql_fetch_assoc($pResult)) {
    $totalPayments += $pRow["payment_amount"];

要按类型获取总计
$totalCash += $pRow["payment_amount"];仅当类型$pRow["payment_type"] = "cash"
$totalCheck += $pRow["payment_amount"];仅当类型$pRow["payment_type"] = "check"
$totalCredit += $pRow["payment_amount"];仅当类型$pRow["payment_type"]= "credit"

最佳答案

你也可以在mysql上做

select type,sum(amount) from table group by type WITH ROLLUP

see GROUP BY Modifiers

10-05 19:25