SQL:连续组成员工

时间:2015-02-05 06:53:31

标签: mysql sql

我想,这很简单。 比方说,我有两个表:[员工]和[项目]

SELECT p.project_name, concat(e.first_name, " ", e.last_name) as employee_name
    FROM project p
    INNER JOIN employee e on e.project_id = p.id
    ORDER by p.project_name

使用SELECT我得到

JohnJane Project | John Doe 
JohnJane Project | Jane Doe
T Project        | Mr. T

但我希望将名称逗号分隔成一行:

 JohnJane Project | John Doe, Jane Doe
  T Project | Mr. T

我该怎么做?

由于 哈德

1 个答案:

答案 0 :(得分:3)

按项目分组并使用GROUP_CONCAT

SELECT p.project_name, 
       group_concat(concat(e.first_name, " ", e.last_name)) as employee_name
FROM project p
INNER JOIN employee e on e.project_id = p.id
GROUP BY p.project_name
ORDER by p.project_name