Mysql Sum Column

时间:2016-09-25 20:18:18

标签: php mysql sum row

我是mysql的新手所以我试图为我的工作制定付款时间表。我需要将总数显示为"付款"。

<?php

        $sql = "SELECT id, DATE_FORMAT(datum, '%d/%m/%Y'), tijd, uren, floor(uren*loon) as payment FROM uren_gewerkt";

        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            echo "<table><tr><th>Datum</th><th>Tijden</th><th>Uren</th><th>Loon</th></tr>";
            // output data of each row
            while($row = $result->fetch_assoc()) {
                echo "<tr><td>".$row["DATE_FORMAT(datum, '%d/%m/%Y')"]."</td><td>".$row["tijd"]."</td><td>".$row["uren"]."</td><td>&euro;".$row["payment"]."</td></tr>";
            }
            echo "</table>";

            echo "Totaal:"; //Sum of payment goes here

        } 
        else {
            echo "0 results";
        }
    ?>

注意&#34; Loon&#34; (从楼层(uren * loon)是固定数字(9.56)

image of table

1 个答案:

答案 0 :(得分:1)

SELECT SUM(floor(uren*loon)) as total FROM uren_gewerkt

或者在PHP中执行:

$total = 0;
//here goes your while loop
while($row = $result->fetch_assoc()) {
    echo "<tr><td>".$row["DATE_FORMAT(datum, '%d/%m/%Y')"]."</td><td>".$row["tijd"]."</td><td>".$row["uren"]."</td><td>&euro;".$row["payment"]."</td></tr>";
    $total += $row["payment"];
}
echo "Totaal: &euro;".$total;