Mysql无法获取聚合查询

时间:2015-05-19 12:11:18

标签: mysql

我有一个表格,其值如下(MySql):

SQL> SELECT deptno, ename FROM emp ORDER BY deptno, ename;

DEPTNO ENAME
------ ----------
    10 CLARK
    10 KING
    10 MILLER
    20 ADAMS
    20 FORD
    20 JONES
    20 SCOTT
    20 SMITH
    30 ALLEN
    30 BLAKE
    30 JAMES
    30 MARTIN
    30 TURNER
    30 WARD

14 rows selected.

但我需要以下不太方便的格式:

DEPTNO ENAME
------ -----------------------------------------
    10 CLARK, KING, MILLER
    20 ADAMS, FORD, JONES, SCOTT, SMITH
    30 ALLEN, BLAKE, JAMES, MARTIN, TURNER, WARD

请帮助我达到以上结果/输出。

1 个答案:

答案 0 :(得分:3)

使用group_concat

SELECT deptno, group_concat(ename order by ename) as ename
FROM emp 
group by deptno
ORDER BY deptno;