Вопрос был помечен MySQL для этого ответа.
Это группа и group_concat()
:
select cu.compId, group_concat(co.name order by co.id) as coopnames
from connectouser cu join
coop co
on cu.coopID = co.ID
group by cu.compId;
В SQL Server вы можете сделать:
select cu.compId,
stuff( (select ',' + co.name
from coop co
where cu.coopID = co.ID
order by co.id
for xml path ('')
), 1, 1, ''
) as names
from connectouser cu;
Самая последняя версия SQL Server поддерживает string_agg()
, что является гораздо лучшим подходом.