获取唯一字段的总和

时间:2013-09-26 14:21:46

标签: mysql select sum

假设我想获得ID,价格和SUM(价格),所以它应该如下所示:

ID    price    sum
--------------------
 1     10      
 1     10      20
 2     20      
 2     20      40
 3     30
 3     30      60

实现这一目标的方法是什么?我真实的疑问:

SELECT users.login, projects.name, time_entries.issue_id, time_entries.hours
FROM users, time_entries
INNER JOIN projects ON time_entries.project_id = projects.id
WHERE time_entries.spent_on = CURDATE() - 1
AND time_entries.user_id = users.id
ORDER BY users.login;

显示

+-------+----------------------------------------------------+----------+-------+
| login | name                                               | issue_id | hours |
+-------+----------------------------------------------------+----------+-------+
| ach   | name1                                              |    12624 |     8 |
| aco   | name2                                              |    11550 |     3 |
| aco   | name2                                              |    11585 |     3 |
| alt   | name3                                              |    12644 |   7.5 |
| ata   | name4                                              |    12761 |     1 |
| ata   | name5                                              |     NULL |     1 |
| ata   | name6                                              |    12790 |   0.5 |
| ata   | name7                                              |    12677 |   5.5 |
| ato   | name8                                              |    12530 |     8 |
| elb   | name8                                              |    12697 |     1 |
| elb   | Software management                                |    12678 |     7 |

我希望获得第五列中的小时数,以便进行唯一登录。

3 个答案:

答案 0 :(得分:0)

我相信你在这里寻找的是GROUP BY

SELECT
    users.login, SUM(time_entries.hours) AS hours
FROM
    users
    JOIN time_entries ON time_entries.user_id = users.id
WHERE
    time_entries.spent_on = CURDATE() - 1
GROUP BY
    users.login

这将使输出类似于:

login    hours
--------------
 ach     42
 alt     13
 ata     27

如果您想要包含issue_id列,根据您的示例输出,它们可能会有所不同,您可能会对GROUP_CONCAT()感兴趣,SELECT users.login, GROUP_CONCAT(issue_id) AS issues, SUM(time_entries.hours) AS hours ... 将提供给定的每个问题的逗号分隔列表用户已经:

{{1}}

答案 1 :(得分:0)

SELECT users.login, projects.name, time_entries.issue_id, SUM(time_entries.hours) as hours
FROM users, time_entries
INNER JOIN projects ON time_entries.project_id = projects.id
WHERE time_entries.spent_on = CURDATE() - 1
AND time_entries.user_id = users.id GROUP BY users.login
ORDER BY users.login;

答案 2 :(得分:0)

newfurniturey,感谢您的回答并感谢其他人。 那是我最接近的:

SELECT users.login, GROUP_CONCAT(projects.name),
GROUP_CONCAT(time_entries.issue_id),
GROUP_CONCAT(time_entries.hours), SUM(time_entries.hours) as hours 
FROM users, time_entries 
INNER JOIN projects ON time_entries.project_id = projects.id
WHERE time_entries.spent_on = CURDATE() - 1 
AND time_entries.user_id = users.id
GROUP BY users.login ORDER BY users.login;

    | ata   | name1,name2,name3 | 12761,12790,12677 | 1,1,0.5,5.5 | 8 |

但是如果可能的话,我想得到下一个结果:

| ata   | name1 | 12761 | 1   |   |
| ata   | name2 | 12790 | 1   |   |
| ata   | name3 | 12677 | 0.5 |   |
| ata   | name3 | 12677 | 5.5 | 8 |

| ata   | name1 | 12761 | 1   |
| ata   | name2 | 12790 | 1   |
| ata   | name3 | 12677 | 0.5 |
| ata   | name3 | 12677 | 5.5 |
| ata   |       |       |     | 8 |