簡體   English   中英

如何獲得特定列中值的數值總和?

[英]How to get numerical sum of values in a particular column?

我正在嘗試獲取我所有庫存的總金額。 項目行設置如下:

store_items

id     stockNumber     priceTotal
3      123             20.00
4      456             15.00

因此,我正在嘗試運行一個查詢,該查詢將返回一個等於 35.00 的結果。

$results = $db->query("SELECT SUM(`priceTotal`) as `sum` FROM `store_items`");

print_r($results);

這只是給我:

mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 2 [type] => 0 )

如何調整查詢以實際添加每一行的值?

我相信這是您正在尋找的查詢。

您需要按特定列進行分組。

$results = $db->query("SELECT SUM(priceTotal) as `sum` FROM `store_items` GROUP BY stockNumber");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM