我有一个包含以下值的表:
id employee_id project_id
1 1 1
2 2 1
3 3 1
4 4 1
我如何按project_id列出?使用mysql查询?
project_id employee_id employee_id employee_id employee_id
1 1 2 3 4
答案 0 :(得分:1)
尝试以下查询 -
SELECT project_id ,SUBSTRING_INDEX(GROUP_CONCAT(employee_id),',',1) AS employee_id1,SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(employee_id),',',2),',',-1) AS employee_id2, SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(employee_id),',',3),',',-1) AS employee_id3, SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(employee_id),',',4),',',-1) AS employee_id4 FROM my_table GROUP BY project_id;
答案 1 :(得分:0)
SELECT project_id,GROUP_CONCAT(employee_id)FROM Table GROUP BY project_id;
可以考虑.. ??