我已经有了这个代码,但是我想知道如何或者是否可以扩展查询以便我可以总结每个用户的燃气使用量,这样我就可以看到谁花了最多的燃料而不是现在我得到他们得到的每一笔补充品的总清单。
<?php
include 'connection.php';
// Check if session is not registered, redirect back to main page.
// Put this code in first line of web page.
session_start();
if (!isset($_COOKIE["user"])) {
header("location:login.php");
}
include 'menu.php';
//start the blue background and the user bar on top of the page
echo "<div class=\"bluebox\">";
echo "<div class=\"datagrid\"><table width=\"100%\" cellspacing=\"0\" cellpadding=\"5\" border=\"0\">";
echo "<thead><tr><th>Brukernavn</th><th>Fullt navn</th><th>Sted</th><th>Liter</th><th>Dato</th></tr></thead><tbody>";
$color1 = "#E1EEf4";
$color2 = "#FFFFFF";
$row_count = 0;
function getContent() {
include 'connection.php';
$query = "SELECT * FROM members JOIN diesel ON diesel.userid = members.id ORDER BY diesel.liter DESC";
$sql=$oDB->prepare($query);
$sql->execute();
$row = $sql->fetchAll();
return $row;
//Row Colors setting
}
error_reporting(E_ALL); ini_set('display_errors', 1);
$data = getContent();
foreach($data as $row) {
$name = $row['username'];
$fname = $row['name'];
$sted = $row['sted'];
$liter = $row['liter'];
$dato = $row['dato'];
$row_color = ($row_count % 2) ? $color1 : $color2;
print "<tr bgcolor=\"$row_color\"><td>".$name."</td><td>".$fname."</td><td>".$sted."</td><td>".$liter."</td><td>".$dato."</td></tr>";
$row_count++;
}
echo "</tbody></table></div>";
?>
感谢阅读:)复活节快乐!
答案 0 :(得分:0)
使用SUM()
和GROUP BY
非常容易。
SELECT
m.username,
sum(d.liter) AS TOT_LITERS
FROM
members m
JOIN
diesel d ON (d.userid = m.id)
GROUP BY m.username
ORDER BY 2 DESC;