2011-05-18 48 views
4

有列的表格:(PROJECT_ID,名)SQL字符串作爲貓聚合函數:貓多個記錄我一起

它的人的名單,每一個與它所屬的項目。如果一個人在兩個項目中,它是重複的。

我想提取一個列的列表:(project_id,people)其中人是一個字符串貓的所有人在該項目上工作的名稱。貓必須用逗號隔開,像這樣:

12, john 
12, mark 
12, dave 
14, luke 

成爲

12, "john, mark, dave" 
14, "luke" 

回答

8

,你可以,如果你堅持有逗號後的空間用一個簡單的查詢

SELECT project_id, GROUP_CONCAT(name) as people 
FROM table 
GROUP BY project_id 

做到這一點:

SELECT project_id, GROUP_CONCAT(name SEPARATOR ", ") as people 
FROM table 
GROUP BY project_id 
+1

'SELECT ... GROUP_CONCAT(名稱分隔符 '')的人...' – 2011-05-18 09:31:32