邢栋博客

邢栋博客,Action博客,记录工作和生活中的点点滴滴

mysql之group_concat()函数

1、功能:将group by产生的同一个分组中的值连接起来,返回一个字符串结果。

2、语法:group_concat( [distinct] 要连接的字段 [order by 排序字段 asc/desc ] [separator '分隔符'] )

说明:通过使用distinct可以排除重复值;如果希望对结果中的值进行排序,可以使用order by子句;separator是一个字符串值,缺省为一个逗号。

3、举例:
表 table_a ,字段 id name cid
表 table_b ,字段 id title
表 table_b 的 id 对应 table_a中的cid

select b.*,(select group_concat(a.name) from table_a a where a.cid = b.id) names from table_b b

select b.*,(select group_concat(a.name order by a.id desc separator '_') from table_a a where a.cid = b.id) names from table_b b


最新微语