• 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()函数的更多相关文章

  1. mysql中的concat,concat_ws(),group_concat()

    mysql中的concat,concat_ws(),group_concat() 说明: 本文中使用的例子均在下面的数据库表tt2下执行:     一.concat()函数 1.功能:将多个字符串连接 ...

  2. mysql中判断字段为空

    mysql中判断字段为null或者不为null   在mysql中,查询某字段为空时,切记不可用 = null, 而是 is null,不为空则是 is not null   select nulco ...

  3. 【mysql】在mysql中更新字段的部分值,更新某个字符串字段的部分内容

    在mysql中更新字段的部分值,更新某个字符串字段的部分内容 sql语句如下: update goods set img = REPLACE(img,'http://ozwm3lwui.bkt.clo ...

  4. mysql中时间字段datetime怎么判断为空和不为空

    mysql中时间字段datetime怎么判断为空和不为空一般为空都用null表示,所以一句sql语句就可以.select * from 表名 where 日期字段 is null;这里要注意null的 ...

  5. mysql中计算两个日期的时间差函数TIMESTAMPDIFF用法

    mysql中计算两个日期的时间差函数TIMESTAMPDIFF用法: 语法: TIMESTAMPDIFF(interval,datetime_expr1,datetime_expr2) 说明: 返回日 ...

  6. mysql函数之四:concat() mysql 多个字段拼接

    语法: COUNT(DISTINCT expr ,[expr ...]) 函数使用说明:返回不同的非NULL 值数目.若找不到匹配的项,则COUNT(DISTINCT) 返回 0 Mysql的查询结果 ...

  7. MySQL中函数CONCAT及GROUP_CONCAT函数的使用

    一.CONCAT()函数 CONCAT()函数用于将多个字符串连接成一个字符串. 以数据表[user]作为实例: SELECT USER_NAME, SEX FROM USER WHERE USER ...

  8. mysql 多个字段拼接

    Mysql的查询结果行字段拼接,能够用以下两个函数实现: 1. concat函数 mysql> select concat('1','2','3') from test ; +--------- ...

  9. mysql多个字段拼接

    Mysql的查询结果行字段拼接,可以用下面两个函数实现: 1. concat函数 mysql') from test ; +---------------------+ ') | +--------- ...

随机推荐

  1. SpringMVC 环境搭建

    SpringMVC 框架环境搭建操作步骤如下: 创建动态 Web 项目 配置 Tomcat 服务器 配置 SpringMVC 前端控制器 <?xml version="1.0" ...

  2. 程序"三高"解决方案

    0. 程序三高 1. 缓存 2. 预处理和延后处理 3. 池化 3.1 内存池 3.2 线程池 3.3 连接池 4. 异步(回调) 5. 消息队列 5.1 服务解耦 5.2 异步处理 5.3 流量削峰 ...

  3. systemverilog动态数组

  4. Archlinux常用软件推荐 更新于2021年4月

    记录一下常用软件 必装软件 包管理工具 yay 代替pacman的包管理 yaourt 备用 终端工具 zsh oh-my-zsh-git 搭配zsh利器` proxychains4 终端代理工具` ...

  5. Linux - last 命令

    前言 为啥写这篇?因为听 grep.sed 教程的时候有这个命令 栗子 加上工作中,运维给我排查问题的时候也用到了,感觉挺重要,先了解为敬! 命令作用 显示上次登录用户的列表 这个是在 Linux 下 ...

  6. harbor搭建及使用

    harbor搭建及使用   1 系统及软件版本 1.1 系统版本 # uname -a Linux localhost.localdomain 3.10.0-862.el7.x86_64 #1 SMP ...

  7. 【NX二次开发】Block UI 线宽

    属性说明 常规         类型 描述     BlockID     String 控件ID     Enable     Logical 是否可操作     Group     Logical ...

  8. FlinkSQL写入Kafka/ES/MySQL示例-JAVA

    一.背景说明 Flink的API做了4层的封装,上两层TableAPI.SQL语法相对简单便于编写,面对小需求可以快速上手解决,本文参考官网及部分线上教程编写source端.sink端代码,分别读取s ...

  9. JAVA微服务应用(1)--SpringBoot中的REST API调用(学习笔记)

    好长时间没有写学习小结了,最近宁正好看了小马哥的微服务系列之<Spring Boot>系列,颇有收获,并且公司也布置一个课题就是关于Spring中的REST API调用.于是乎回归本行,再 ...

  10. 惊呆了,Spring Boot居然这么耗内存!

    Spring Boot总体来说,搭建还是比较容易的,特别是Spring Cloud全家桶,简称亲民微服务,但在发展趋势中,容器化技术已经成熟,面对巨耗内存的Spring Boot,小公司表示用不起.如 ...