1、concat()
1.1、用法:concat(str1,str2,str3,…)
连接参数的函数,返回结果为连接参数的字符串。如果有一个参数为NULL
,则返回的结果为NULL
。
1.2、示例
concat('a', 'b', 'c')
concat('a', null, 'c')
2、concat_ws()
2.1、用法:concat_ws('分隔符', str1, str2, …)
concat()
的一个特殊形式,表示concat with separator
,两个参数之间加上特定的分隔符。返回的是用指定分隔符连接参数的字符串。如果分割符为null
,则返回null
,参数为null
,则忽略该参数。
2.2、示例
concat_ws("/", "2018", "12", "19")
concat_ws(":", "22", "47", null)
concat_ws(null, "22", "47")
3、group_concat()
3.1、用法:group_concat(str1, [order by str3], [separator '分隔符'])
把相同组的字符串以特定分隔符连接为一行。
3.2、示例
3.2.1、数据
| |
---|
id | name |
1 | bob |
1 | anna |
1 | helen |
2 | tom |
2 | baby |
2 | tom |
3.2.2、按id
分组,把name
连接为1行
select id, group_concat(name)
3.2.3、按id
分组,把name
连接为一行,并按name
升序
select id,group_concat(name order by name asc)
3.2.4、按id
分组,name
去重并连接为一行,按name
升序,用逗号分隔
select id,group_concat(name order by name asc)