Maybe you need group_concat() in MYSQL.
Noticing that in MYSQL you will need a sample:
* SQLFIDDLE demo
Select department, group_concat(name,',') as nameList from foo group by department ;
Results:
Department NameList D1 John, Mary D2 Tim, Dan, Jack D3 Kate, Felix
The following is the usage method in TSQL:
You can try the following sample code and configure it for your table / columns:
SELECT department, namelist = STUFF( (SELECT ','+ Name FROM foo B WHERE b.department = a.department FOR XML PATH('')) , 1 , 1 , '' ) FROM foo A
Or you can do CTE .
source share