MySQL 中 concat 函数
mysql 函数 CONCAT
2023-06-13 09:11:24 时间
大家好,又见面了,我是你们的朋友全栈君。
MySQL 中 concat 函数
MySQL 中 concat 函数
语法:concat(str1,str2,…)
注意:返回结果为连接参数产生的字符串,如果有任何一个参数为 NULL,则返回值为 NULL。
select concat("a","b","c");
输出:abc
注:
Mysql 的 concat 函数在连接字符串的时候,只要其中一个为 NULL
则返回值为 NULL.
select concat("1","2",null);
输出结果:NULL
MySQL 中 concat_ws 函数
语法:concat_ws(separator,str1,str2)
concat_ws() 代表 Concat With Separator,是 CONCAT() 的特殊形式,第一个参数是其他参数的分隔符。分隔符的位置放在要连接的两个字符串之间,分隔符可以是一个字符串,也可以是一个其他参数。
注意:如果分隔符为 NULL ,则结果为 NULL 。函数会忽略任何分隔符参数后的 NULL 值。
eg:字符串连接后以逗号分隔
select concat_ws(",","1","2","3");
输出结果: 1,2,3
与 Mysql 中 concat 函数不同的是,concat_ws 函数在执行的时候,
不会因为 NULL 值而返回 NULL。
select concat_ws(",","1","2",NULL);
输出结果: 1.2
MySQL 中 group_concat 函数
语法:group_concat([DISTINCT]) 要连接的字段 [order by asc/desc 排序字段] [Separator ‘分隔符’]
eg: tableTest:有 id name 字段
select * from tableTest;
输出结果:
id name
1 10
1 20
1 20
2 20
3 200
3 500
1. 以 id 分组,把 name 字段的值打印在一起,逗号分隔(默认)
select id, group_concat(name) from tableTest group by id;
输出结果:
id group_concat(name)
1 10,20,20
2 20
3 200,500
2. 以 id 分组,把 name 字段的值打印在一行,分号分隔。
select id,group_concat(name separator';') from tableTest group by id;
id group_concat(name separator';')
1 10;20;20
2 20
3 200;500
3. 以 id 分组,把冗余的 name 字段的值打印在一行,逗号分隔。
select id,group_concat(distinct name) from tableTest group by id;
输出结果:
id group_concat(distinct name)
1 10,20
2 20
3 200,500
4. 以 id 分组,把 name 字段的值打印在一行,逗号分隔,以 name 排倒序
select id,group_concat(name order by name desc) from tableTest group by id;
输出结果:
id group_concat(name order by name desc)
1 20,20,10
2 20
3 500,300
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/138920.html原文链接:https://javaforall.cn
相关文章
- 深入浅出Mysql:合并多个列查询(mysql查询合并列)
- MySQL中的窗口函数使用指南(窗口函数mysql)
- 提升MySQL运行效率:利用时间函数(mysql运行时间函数)
- MySQL中行列函数的应用(mysql行列函数)
- 使用Mac终端进入MySQL数据库(mac终端进入mysql)
- MySQL实现批量创建表:快速高效的数据库表管理方法(mysql批量创建表)
- 去向MySQL:新的开发数据存储之旅(go mysql)
- MySQL中row函数详解(mysql中row函数)
- MySQL中的IF函数,用法详解(mysql 中if 等于)
- 在cmd中如何进入MySQL交互模式(cmd怎样进去mysql)
- MySQL中常用加法函数的使用方法(mysql中加法函数)
- 使用MySQL将一行数据转换为多行文本的方法(mysql一行转为多行)
- MySQL中的XML配置方式详解(mysql xml配置c)