完整语法:
group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator ‘分隔符’])
SELECT * FROM `sqltest`
如果按照group_id分组,会自动去重
SELECT * FROM `sqltest` GROUP BY group_id
想要类别按组分类集中到一起,就要用Group_concat
SELECT group_id,GROUP_CONCAT(category) FROM `sqltest` GROUP BY group_id
还有几种扩展:
①可以给类别去重
SELECT group_id,GROUP_CONCAT(DISTINCT category) FROM `sqltest` GROUP BY group_id
②给类别排序
SELECT group_id,GROUP_CONCAT(category ORDER BY category DESC) FROM `sqltest` GROUP BY group_id
③给类别更改分隔符
SELECT group_id,GROUP_CONCAT(category SEPARATOR '_') FROM `sqltest` GROUP BY group_id
到此这篇关于SQL函数实现Group_concat用法的文章就介绍到这了,更多相关SQL Group_concat内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!