MySQL中的字段拼接 concat() concat_ws() group_concat()函数
- 1.concat()函数
- 2.concat_ws()函数
- 3.group_concat()函数
操作的table
select * from test_concat order by id limit 5;

1.concat()函数
功能:将多个字符串连接成一个字符串。
语法:concat(str1, str2,...),返回结果为连接参数产生的字符串,如果有任何一个参数为null,则返回值为null。
3、举例:
select concat(area,fr,best_history_data) from test_concat order by id limit 5;

如果想在字段间加分隔符,需要在每两个字段间都增加一个分隔符,比较麻烦:
select concat(area,',',fr,',',best_history_data) as test_result from test_concat order by id limit 5;

2.concat_ws()函数
功能:和concat()一样,将多个字符串连接成一个字符串,但是可以一次性指定分隔符~(concat_ws就是concat with separator)
语法:concat_ws(separator, str1, str2, ...)
说明:第一个参数指定分隔符。需要注意的是分隔符不能为null,如果为null,则返回结果为null。
select concat_ws(',',area,fr,best_history_data) from test_concat order by id limit 5;

注意:和MySQL中concat函数不同的是, concat_ws函数在执行的时候,不会因为NULL值而返回NULL
mysql> select concat_ws(',','11','22',NULL);
+-------------------------------+
| concat_ws(',','11','22',NULL) |
+-------------------------------+
| 11,22 |
+-------------------------------+
1 row in set (0.00 sec)
3.group_concat()函数
功能:将group by产生的同一个分组中的值连接起来,返回一个字符串结果。
语法:group_concat( [distinct] 要连接的字段 [order by 排序字段 asc/desc ] [separator '分隔符'] )
通过使用distinct可以排除重复值;如果希望对结果中的值进行排序,可以使用order by子句;separator是一个字符串值,缺省为一个逗号。
测试table:
select * from test_table;

1.根据area分组,拼接每个区域各个指标的指标值
select group_concat(fr,best_history_data) from test_table group by area;

2.增加分割符
select group_concat(fr,best_history_data separator '|') from test_table group by area;

3.结合concat_ws()函数,在fr与best_history_data之间增加分割符-
select group_concat(concat_ws('-',fr,best_history_data) separator '|') from test_table group by area;

4.根据best_history_data进行排序
select group_concat(concat_ws('-',fr,best_history_data) order by best_history_data desc separator '|') from test_table group by area;

MySQL中的字段拼接 concat() concat_ws() group_concat()函数的更多相关文章
- mysql中的concat,concat_ws(),group_concat()
mysql中的concat,concat_ws(),group_concat() 说明: 本文中使用的例子均在下面的数据库表tt2下执行: 一.concat()函数 1.功能:将多个字符串连接 ...
- mysql中判断字段为空
mysql中判断字段为null或者不为null 在mysql中,查询某字段为空时,切记不可用 = null, 而是 is null,不为空则是 is not null select nulco ...
- 【mysql】在mysql中更新字段的部分值,更新某个字符串字段的部分内容
在mysql中更新字段的部分值,更新某个字符串字段的部分内容 sql语句如下: update goods set img = REPLACE(img,'http://ozwm3lwui.bkt.clo ...
- mysql中时间字段datetime怎么判断为空和不为空
mysql中时间字段datetime怎么判断为空和不为空一般为空都用null表示,所以一句sql语句就可以.select * from 表名 where 日期字段 is null;这里要注意null的 ...
- mysql中计算两个日期的时间差函数TIMESTAMPDIFF用法
mysql中计算两个日期的时间差函数TIMESTAMPDIFF用法: 语法: TIMESTAMPDIFF(interval,datetime_expr1,datetime_expr2) 说明: 返回日 ...
- mysql函数之四:concat() mysql 多个字段拼接
语法: COUNT(DISTINCT expr ,[expr ...]) 函数使用说明:返回不同的非NULL 值数目.若找不到匹配的项,则COUNT(DISTINCT) 返回 0 Mysql的查询结果 ...
- MySQL中函数CONCAT及GROUP_CONCAT函数的使用
一.CONCAT()函数 CONCAT()函数用于将多个字符串连接成一个字符串. 以数据表[user]作为实例: SELECT USER_NAME, SEX FROM USER WHERE USER ...
- mysql 多个字段拼接
Mysql的查询结果行字段拼接,能够用以下两个函数实现: 1. concat函数 mysql> select concat('1','2','3') from test ; +--------- ...
- mysql多个字段拼接
Mysql的查询结果行字段拼接,可以用下面两个函数实现: 1. concat函数 mysql') from test ; +---------------------+ ') | +--------- ...
随机推荐
- mysql数据库-备份与还原-Percona XtraBackup 2.4备份工具使用
目录 xtrabackup 特点 备份生成的相关文件 xtrabackup 安装 xtrabackup 用法 1 备份 2 预备份 3 还原 4 其他 还原注意事项 xtrabackup实现完全备份及 ...
- 启动dubbo消费端过程提示No provider available for the service的问题定位与解决
文/朱季谦 某次在启动dubbo消费端时,发现无法从zookeeper注册中心获取到所依赖的消费者API,启动日志一直出现这样的异常提示 Failed to check the status of t ...
- java IO教程《四》
properties使用 什么是Properties? Properties(Java.util.Properties),该类主要用于读取Java的配置文件,不同的编程语言有自己所支持的配置文件,配置 ...
- 深度学习数据特征提取:ICCV2019论文解析
深度学习数据特征提取:ICCV2019论文解析 Goal-Driven Sequential Data Abstraction 论文链接: http://openaccess.thecvf.com/c ...
- pytorch生成对抗示例
pytorch生成对抗示例 本文对ML(机器学习)模型的安全漏洞的认识,并将深入了解对抗性机器学习的热门话题.图像添加难以察觉的扰动会导致模型性能大不相同.通过图像分类器上的示例探讨该主题.使用第一种 ...
- Keras神经网络集成技术
Keras神经网络集成技术 create_keras_neuropod 将Keras模型打包为神经网络集成包.目前,上文已经支持TensorFlow后端. create_keras_neuropod( ...
- 嵌入式Linux的OTA更新,基础知识和实现
嵌入式Linux的OTA更新,第1部分-基础知识和实现 OTA updates for Embedded Linux, Fundamentals and implementation 更新的需要 一 ...
- jvm调优神器——arthas
在上一篇<jvm调优的几种场景>中介绍了几种常见的jvm方面调优的场景,用的都是jdk自带的小工具,比如jps.jmap.jstack等.用这些自带的工具排查问题时最大的痛点就是过程比较麻 ...
- vue3.0搭建项目
安装node.js 查看版本node -v 安装vue3.0版本之前需要先把vue-cli升级到4.0版本, 如果之前安装过vue-cli需要把2.0相关的卸载之后重新安装,npm uni -g vu ...
- [Linux]经典面试题 - 系统管理 - 备份策略
[Linux]经典面试题 - 系统管理 - 备份策略 目录 [Linux]经典面试题 - 系统管理 - 备份策略 一.备份目录 1.1 系统目录 1.2 服务目录 二.备份策略 2.1 完整备份 2. ...