我使用while
循环来处理表记录,如下所示:
$get_s = "SELECT * FROM sells ORDER BY sells_date";
if ($result = mysqli_query($AECONNECT, $get_s)) {
while ($show_s = mysqli_fetch_assoc($result)) {
$quantity = $show_s['sells_quantity'];
}
mysqli_free_result($result);
}
我有我的所有桌面记录,现在我想总结所有数量字段,但我不知道该怎么做。
例如,如果我为2, 1, 5, 1, 3, 6
等记录获得了10个记录数量,我想这样总结:2+1+5+1+3+6 = 18
答案 0 :(得分:1)
如果你可以在mysql中做点什么 - 做吧。使用SUM
聚合函数:
$get_s = "SELECT SUM(sells_quantity) as sells_sum FROM sells ORDER BY sells_date";
if ($result = mysqli_query($AECONNECT, $get_s)) {
$show_s = mysqli_fetch_assoc($result);
echo $show_s['sells_sum'];
}
mysqli_free_result($result);
但是,如果您需要某些行的值 - 您可以在循环中计算总和:
$get_s = "SELECT * FROM sells ORDER BY sells_date";
if ($result = mysqli_query($AECONNECT, $get_s)) {
$total = 0;
while ($show_s = mysqli_fetch_assoc($result)) {
$quantity = $show_s['sells_quantity'];
$total += $quantity;
}
mysqli_free_result($result);
echo $total;
}
但是mysql SUM()
是可取的。