MySQL字段拼接的三个函数

MySQL中如果对字段有拼接需求,可以利用原生提供的三个函数,功能虽然相近,但细节略有不同,针对不同的场景,选择不同的方案,
  • concat()
  • concat_ws()
  • group_concat()
1. concat()函数

可以实现多个字段使用空字符串拼接为一个字段,如下所示,

mysql> select concat(id, type) from mm_content limit 10;+------------------+| concat(id, type) |+------------------+| 100818image   || 100824image   || 100825video   || 100826video   || 100827video   || 100828video   || 100829video   || 100830video   || 100831video   || 100832video   |+------------------+10 rows in set (0.00 sec)
但是,如果有字段值为NULL,则结果为NULL,
mysql> select concat(id, type, tags) from mm_content limit 10;+------------------------+| concat(id, type, tags) |+------------------------+NULL          |NULL          |NULL          |NULL          |NULL          |NULL          |NULL          |NULL          |NULL          |NULL          |+------------------------+10 rows in set (0.00 sec)
2. concat_ws()函数

concat()函数如果想要使用分隔符分割,就需要每个字段中间插一个字符串,不是非常便捷,但是通过concat_ws()函数可以一次性地解决分隔符的问题,并且不会因为某个值为NULL,而全部为NULL,如下所示,

mysql> select concat_ws(' ', id, type, tags) from mm_content limit 10;+--------------------------------+| concat_ws(' ', id, type, tags) |+--------------------------------+| 100818 image          || 100824 image          || 100825 video          || 100826 video          || 100827 video          || 100828 video          || 100829 video          || 100830 video          || 100831 video          || 100832 video          |+--------------------------------+10 rows in set (0.00 sec)
3. group_concat()函数

正常情况下,这个语句会报错,

mysql> select id from test_user group by age;ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test_user.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
但是group_concat()函数可以将分组状态下的其他字段拼接成字符串查询,如下所示,
mysql> select group_concat(name) from test_user group by age;+--------------------+| group_concat(name) |+--------------------+| wen,ning           || wxnacy,win         |+--------------------+2 rows in set (0.00 sec)
默认使用逗号分隔,我们也可以指定分隔符,
mysql> select group_concat(name separator ' ') from test_user group by age;+----------------------------------+| group_concat(name separator ' ') |+----------------------------------+| wen ning                         || wxnacy win                       |+----------------------------------+2 rows in set (0.00 sec)
还可以将字符串按照某个顺序排列,
mysql> select group_concat(name order by id desc separator ' ') from test_user group by age;+---------------------------------------------------+| group_concat(name order by id desc separator ' ') |+---------------------------------------------------+| ning wen                                          || win wxnacy                                        |+---------------------------------------------------+2 rows in set (0.00 sec)
如果想要拼接多个字段,默认是用空字符串进行拼接的,我们可以利用concat_ws()方法嵌套一层,
mysql> select group_concat(concat_ws(',', id, name) separator ' ') from test_user group by age;+------------------------------------------------------+| group_concat(concat_ws(',', id, name) separator ' ') |+------------------------------------------------------+| 1,wen 2,ning                                         || 3,wxnacy 4,win                                       |+------------------------------------------------------+2 rows in set (0.00 sec)
针对不同的场景,可以选择不同的字符串拼接函数,满足个性化需求,因此,平时积累一些常用的函数,还是有作用的。

参考链接,

https://www.zhuxianfei.com/database/mysql/43287.html


如果您认为这篇文章有些帮助,还请不吝点下文章末尾的"点赞"和"在看",或者直接转发pyq,




近期更新的文章:
MySQL日志 - Undo回滚日志的介绍
MySQL创建用户提示1396
MySQL日志 - Relay Log中继日志的介绍
常见的行业认证和资质清单介绍
球探体系中数字化的应用场景

近期的热文:
推荐一篇Oracle RAC Cache Fusion的经典论文
"红警"游戏开源代码带给我们的震撼

文章分类和索引:
公众号1200篇文章分类和索引

请使用浏览器的分享功能分享到微信等