MySQL中concat,concat_ws以及group_concat

一、concat()函数

1、功能:将(一条记录中的)多个字符串连接成一个字符串。

2、语法:concat(str1, str2,...),字段之间可以指定分隔符

返回结果为连接参数产生的字符串,如果有任何一个参数为null,则返回值为null。

二、concat_ws()函数

1、功能:和concat()一样,将多个字符串连接成一个字符串,但是可以一次性指定分隔符~(concat_ws就是concat with separator)

2、语法:concat_ws(separator, str1, str2, ...)

说明:第一个参数指定分隔符。需要注意的是分隔符不能为null,如果为null,则返回结果为null。

三、group_concat()函数

1、功能:将(多条记录中的)多个字符串连接成一个字符串。

2、语法:group_concat(str1, str2,...),字段之间可以指定分隔符,默认逗号

说明:多条记录的来源通常是分组查询group_by字句,但也可以是where条件

示例:1:select GROUP_CONCAT(goods_name,id)   from   toocms_goods where id >5

        2:select GROUP_CONCAT(goods_name,id)   from   toocms_goods group_by cate_id

猜你喜欢

转载自blog.csdn.net/phptyong/article/details/81179279