【MySQL】MySQL的group_concat使用例子
> 参考的优秀文章
> 简单的例子
此函数的功能,是拼接group分组多行的数据为一行,并可以指定去重、排序、分隔符。
例子:
select t.name, group_concat(distinct t.role order by t.sort asc separator ';') from ( select 'Nick' as name, 'coder' as role, 1 as sort union all select 'Nick' as name, 'admin' as role, 3 as sort union all select 'Nick' as name, 'admin' as role, 2 as sort union all select 'Viki' as name, 'coder' as role, 1 as sort union all select 'Viki' as name, 'admin' as role, 2 as sort union all select 'Viki' as name, 'player' as role, 3 as sort union all select 'Darren' as name, 'admin' as role, 2 as sort union all select 'Darren' as name, 'player' as role, 1 as sort ) t group by t.name;
结果:
作者:Nick Huang 博客:http://www.cnblogs.com/nick-huang/
本博客为学习、笔记之用,以笔记形式记录学习的知识与感悟。学习过程中可能参考各种资料,如觉文中表述过分引用,请务必告知,以便迅速处理。如有错漏,不吝赐教。
如果本文对您有用,点赞或评论哦;如果您喜欢我的文章,请点击关注我哦~
本博客为学习、笔记之用,以笔记形式记录学习的知识与感悟。学习过程中可能参考各种资料,如觉文中表述过分引用,请务必告知,以便迅速处理。如有错漏,不吝赐教。
如果本文对您有用,点赞或评论哦;如果您喜欢我的文章,请点击关注我哦~