2

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

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 |

我想在第五列中获得唯一登录的小时总和。

4

3 回答 3

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 |
于 2013-09-26T15:21:29.393 回答
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
...
于 2013-09-26T14:27:57.107 回答
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;
于 2013-09-26T14:28:15.910 回答