gpt4 book ai didi

mysql - 带条件的 SUM 行的 SQL 语句

转载 作者:行者123 更新时间:2023-11-29 01:13:05 26 4
gpt4 key购买 nike

我有user_id = 23 我想查询,我还有一个表work_log包括project_id, user_id, log_hours 像这样:

+----+------------+---------+-----------+
| id | project_id | user_id | log_hours |
+----+------------+---------+-----------+
| 1 | 23 | 23 | 2.5 |
| 1 | 3 | 23 | 3 |
| 1 | 23 | 23 | 1 |
| 1 | 24 | 23 | 3 |
| 1 | 24 | 23 | 1.5 |
| 1 | 23 | 23 | 0.5 |
+----+------------+---------+-----------+

但现在我想对每个结果的所有 log_hours 求和:

+------------+------------------+
| project_id | sum_of_log_hours |
+------------+------------------+
| 23 | 4 |
| 3 | 3 |
| 24 | 4.5 |
+------------+------------------+

如何编写 SQL 语句来获得该结果?

最佳答案

应该这样做:

Select project_id, sum(log_hours) as sum_log_hours
from work_log
where user_id = '23'
group by project_id

关于mysql - 带条件的 SUM 行的 SQL 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32944395/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com