mysql 获取列总和并一次获取不同的值

mysql get the columns sum and also get the distinct values at a time

我有这样的数据库

id   project_id    client_id    price

1       1           1               200
2       2           1               123
3       2           1               100
4       1           1               87
5       1           1               143
6       1           1               100
7       3           3               123
8       3           3               99  
9       4           3               86
10      4           3               43
11      4           3               145
12      4           3               155

现在,我希望它对具有相同 client_id 的价格列求和。 为此,我只是这样查询

Select  `project_id`, SUM(`price`) FROM `table-name` GROUP BY `client_id`

这是对价格求和,但我在结果中只得到两个 project_id。我希望结果应该是客户 ID 的所有不同项目,并且将为组客户汇总价格。 那么有人可以告诉我该怎么做吗?任何帮助和建议都将非常感激。谢谢

您不应该在 group by 查询中包含不在 group by 语句中的 "bare" 列。

如果你想要项目列表,你可以在这样的列表中获取它们:

SELECT client_id, GROUP_CONCAT(project_id), SUM(price)
FROM table-name
GROUP BY client_id;

你只有两个客户,为什么你只得到两条记录,你可以按两列分组,

Select  `project_id`, SUM(`price`) FROM `table-name` GROUP BY `client_id`, `project_id`