使用group by时如何在单列中获取多个值?

时间:2016-05-12 10:32:28

标签: mysql sql

我当前的查询是

SELECT SUM(error_count) as error_count, job_id, operator_name
from table_name
GROUP BY operator_name

输出

error_count job_id operator_name    
17           12321     A.kumar
11           2212      Bindu
45           3412      Harish Babu

错误计数正确,但作为最后一个job_id的作业ID不正确。我希望所有job_id都以逗号分隔

SELECT SUM(error_count) as error_count , job_id = some_func(""+job_id), operator_name
from table_name
GROUP BY operator_name

所以我可以得到答案

error_count   job_id         operator_name  
    17           12321,123     A.kumar
    11           2212 ,111     Bindu
    45           3412,3412     Harish Babu

我希望这个问题有道理。

2 个答案:

答案 0 :(得分:2)

试试这个,可能有用;)

SELECT SUM(error_count) as error_count , group_concat(job_id) as job_id, operator_name    
FROM table_name
GROUP BY operator_name

另外,看看GROUP BY (Aggregate) Function Descriptions

答案 1 :(得分:1)

尝试Group_Concat,

    SELECT 
            SUM(error_count) AS error_count , 
            GROUP_CONCAT(job_id) AS jobids,
            operator_name 
    FROM
            table_name 
    GROUP BY 
            operator_name