简体   繁体   English

按其他列分组时列的总和

[英]SUM of columns while grouping by other column

So this is the structure of my MySQL table that I wanna work this out with: 这就是我要使用的MySQL表的结构:

 ID type     category_id amount
 12 Expense     3        963.39
 13 Expense     5        1200.50
 14 Expense     3        444.12
 15 Expense     5        1137.56
  ..............................

Desired output: 所需的输出:

 1407,41 (for category_id = 3)
 2338,06 (for category_id = 5)
 ....... (and for other category_id)

What I get now: 我现在得到的是:

 1407,41 (only for category_id = 3)

My query does not add or display the sum of other category_id. 我的查询未添加或显示其他category_id的总和。

This is the query I am trying: 这是我正在尝试的查询:

$query = "SELECT SUM(amount) AS TotalAmount FROM spendee WHERE type = 'Expense' 
group by category_id having count(*) >1 ";
    $expense_query = mysqli_query($connection, $query);
    $expense_count = mysqli_fetch_array($expense_query);
    echo $expense_count[0];

Been stuck with this for the last couple of days. 在过去的几天里一直坚持下去。 Any help is very much appreciated. 很感谢任何形式的帮助。 Thank you! 谢谢!

You're only calling mysqli_fetch_array() once. 您只调用一次mysqli_fetch_array() You need to call it in a loop to get all the totals. 您需要循环调用它以获取所有总数。 You should also include the category ID in the SELECT list. 您还应该在SELECT列表中包括类别ID。

$query = "SELECT category_id, SUM(amount) AS TotalAmount FROM spendee WHERE type = 'Expense' 
            group by category_id having count(*) >1 ";
$expense_query = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($expense_query)) {
    echo "{$row['TotalAmount']} (for category_id = {$row['category_id']}<br>\n";
}

The query here works. 这里的查询有效。 It's just that you only select the first result from the $expense_count variable. 仅从$ expense_count变量中选择第一个结果。 $expense_count[1] will return the second category listed, $expense_count[2 will return the third one, ect... Try echo implode(" <br>", $expense_count); $ expense_count [1]将返回列出的第二个类别,$ expense_count [2将返回第三个类别,以此类推...尝试echo implode(" <br>", $expense_count);

Have a nice day. 祝你今天愉快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP 5.6尝试通过对一列进行分组来对关联数组列求和 - PHP 5.6 Trying to Sum assoc array columns by grouping one column ID列未更新,而其他列正在更新? - ID Column not updating while other columns are? SQL 查询对两列或更多列求和并将它们显示在另一列中 - SQL query to sum two or more columns and displaying them in the other column 具有项目和项目类型分组的列的总和 - Sum of columns with item and item type grouping 选择按天分组的列值总和 - select with sum of column values grouping by day Mongodb按列聚合/分组并计数特定列 - Mongodb aggregating/grouping by column and counting specific columns SQL查询以查找某列值等于同一行中其他两列之和的行 - SQL query to find rows where a column value is equal to the sum of tow other columns in the same row 按一列对二维数组数据进行分组,并对每组中的其他列求和(分别) - Group 2d array data by one column and sum other columns in each group (separately) 其他2列的SQL列减法? - SQL column subtraction of 2 other columns? 我想总结两列的行,然后使用php update将值插入同一个表中的另一列的行。 可能吗? - I want to sum two columns' row and then the value insert the other column's row in the same table using php update. Is it possible?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM