数据库数据:
1 | Hello | 1 |
1 | Mr | 2 |
1 | King | 3 |
2 | I | 1 |
2 | am | 2 |
2 | the | 3 |
2 | Best | 4 |
显示为:
1 Hello,Mr,King
2 I,am,the,Best
代码:
sqlSERVER:
select distinct id, test=stuff((select ','+test from test t where id=tb.id for xml path('')), 1, 1, '')
from test tb
Mysql:
select id,group_concat(test order by rank SEPARATOR ' ')
from table
group by id