[英]MySQL show all rows and sum at the end
關於這個問題,我已經閱讀了大約 20 個不同的答案,但要么我誤解了答案,要么只是沒有點擊。 這是我的情況:
我有一張列出食譜成分的表格。 表中的列有:ingredient_id, ingredient_title, ingredient_oz, ingredient_grams, ingredient_lbs(磅)等。
我想列出每種成分,然后在列出所有成分后,添加最后一行,總結所有盎司、克、磅等。下面是我試圖實現的 output 的示例。
例子:
INGREDIENT TITLE OZ GRAMS LBS
ingredient1 4 6 3
ingredient2 1 2 4
ingredient3 9 4 4
TOTAL 14 12 11
我的第一個想法是簡單地在 SQL 中使用 SUM() AS
SELECT ingredient_title, ingredient_oz, ingredient_lbs, ingredient_grams, SUM(ingredient_oz) as oz_sum, SUM(ingredient_lbs) as lbs_sum, SUM(ingredient_grams) as grams_sum FROM ingredients
這是我頁面上的代碼:
<!-- Beginning of table is here -->
<?php
while ($ingredientRow = $ingredients->fetch_assoc()) { ?>
<tr>
<td><?php echo $ingredientRow["ingredient_title"]; ?></td>
<td><?php echo $ingredientRow["ingredient_oz"]; ?></td>
<td><?php echo $ingredientRow["ingredient_lbs"]; ?></td>
<td><?php echo $ingredientRow["ingredient_grams"]; ?></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<td>TOTALS</td>
<td><?php echo $ingredientRow["oz_sum"]; ?></td>
<td><?php echo $ingredientRow["lbs_sum"]; ?></td>
<td><?php echo $ingredientRow["grams_sum"]; ?></td>
</tr>
</tfoot>
</table>
<?php }?>
然而,它所做的只是返回第一行(成分 1),而不返回剩余的行或總和。 然后當我繼續閱讀這篇文章時,我看到很少有人在討論使用“group by”。 然后我嘗試了:
SELECT ingredient_title, ingredient_oz, ingredient_lbs, ingredient_grams, SUM(ingredient_oz) as oz_sum, SUM(ingredient_lbs) as lbs_sum, SUM(ingredient_grams) as grams_sum FROM ingredients GROUP BY ingredient_title
這將返回所有行,但同樣不會返回總和。 我是按錯誤的字段分組嗎? 我是否需要對要求和的每個字段進行分組?
當你運行一個查詢時,你會得到你要求的數據,所以基本上如果你運行一個查詢來單獨返回所有行 - 你會得到那些,沒有總數。 另一方面,如果您運行查詢以僅獲取總和/總計,則不會獲取單個數據行。
有兩種方法可以得到你想要的東西。 一種是通過查詢完成的,一種是通過 PHP 本身完成的。
您可以編寫一個聯合查詢來獲取各個數據行,然后返回總和,如下所示:
SELECT
ingredient_title,
ingredient_oz,
ingredient_lbs,
ingredient_grams
FROM
ingredients
union all
SELECT
ingredient_title,
SUM(ingredient_oz) as oz_sum,
SUM(ingredient_lbs) as lbs_sum,
SUM(ingredient_grams) as grams_sum
FROM
ingredients
這將返回兩者。
或者,您可以編寫一段 PHP 代碼快速片段,根據查詢的第一部分在代碼中為您添加:
<?php
$sql="SELECT
ingredient_title,
ingredient_oz,
ingredient_lbs,
ingredient_grams
FROM
ingredients";
//Execute query:
while($result)
{
echo $result['ingredient_title'];
echo $result['ingredient_oz'];
// etc etc. Format as needed...
$ingOz+=$result['ingredient_oz'];
$ingLbs+=$result['ingredient_lbs'];
$ingGrams+=$result['ingredient_grams'];
}
// And now the totals:
echo $ingOz;
echo $ingLbs;
// etc etc.
?>
我個人可能會使用第二種方法 - 你不需要讓數據庫運行兩次查詢來獲得結果 - 而且你已經獲得了所有單獨的數據行,因此你也可以簡單地保持一個簡單的運行總計在一個變量中,根據需要顯示。
不要害怕運行 2 個單獨的 SQL 查詢,因為:
$query = "
SELECT
ingredient_title,
ingredient_oz,
ingredient_lbs,
ingredient_grams
FROM ingredients
";
$ingredients = $db->query($query)->fetch_all();
$query = "
SELECT
SUM(ingredient_oz) as oz_sum,
SUM(ingredient_lbs) as lbs_sum,
SUM(ingredient_grams) as grams_sum
FROM ingredients
";
$ingredientSummary = $db->query($query)->fetch_assoc();
<!-- Beginning of table is here -->
<?php
foreach ($ingredients as $ingredientRow) ?>
<tr>
<td><?php echo $ingredientRow["ingredient_title"]; ?></td>
<td><?php echo $ingredientRow["ingredient_oz"]; ?></td>
<td><?php echo $ingredientRow["ingredient_lbs"]; ?></td>
<td><?php echo $ingredientRow["ingredient_grams"]; ?></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<td>TOTALS</td>
<td><?php echo $ingredientSummary["oz_sum"]; ?></td>
<td><?php echo $ingredientSummary["lbs_sum"]; ?></td>
<td><?php echo $ingredientSummary["grams_sum"]; ?></td>
</tr>
</tfoot>
</table>
<?php }?>
我認為您應該在選擇總和時將其設為單獨的查詢。 當總和是所有行的總和時,它可以獲得每行的總和是否有意義? 通過分組,您可以使 SUM 對每個組求和,並且每個成分都被標記為一個組。
有點晚了,但是在查詢結束時回滾很容易
SELECT ingredient_title, ingredient_oz, ingredient_lbs, ingredient_grams
FROM ingredients
WITH ROLL BACK
這里有一些來自其他網站的例子https://www.mysqltutorial.org/mysql-rollup/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.