当前位置:网站首页>concat和concat_ws()区别及group_concat()和repeat()函数的使用
concat和concat_ws()区别及group_concat()和repeat()函数的使用
2022-07-03 13:35:00 【大数据小王】
1 :concat 函数可以连接一个或者多个字符串,如
select concat('10');//10
select concat('11','22','33');//1122332:concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL
select concat('11','22',null);//null3:concat_ws()函数,
表示concat with separator,即有分隔符的字符串连接
select concat_ws(',','11','22','33');//11,22,33
select concat_ws('|','11','22','33');//11|22|33
select concat_ws('*','11','22',NULL);//11*22和concat不同的是, concat_ws函数在执行的时候,不会因为NULL值而返回NULL
4:
group_concat()
可用来行转列
完整的语法如下:
group_concat([DISTINCT] 要连接的字段[Order BY ASC/DESC 排序字段] [Separator ‘分隔符’])
例子:
create table aa(
id int,
name VARCHAR(255)
);
insert into aa values(1,10);
insert into aa values(1,10);
insert into aa values(1,20);
insert into aa values(1,30);
insert into aa values(3,30);
insert into aa values(5,60);
insert into aa values(5,90);
insert into aa values(6,990);
1 以id分组,把name字段的值打印在一行,逗号分隔(默认)
select id,group_concat(name) from aa group by id;
2 以id分组,把name字段的值打印在一行,分号分隔
select id,group_concat(name separator ';') from aa group by id;
3 以id分组,把去冗余的name字段的值打印在一行,逗号分隔
select id,group_concat(distinct name separator ';') from aa group by id;
4 以id分组,把name字段的值打印在一行,*号分隔,以name排倒序
select id,group_concat(name order by name desc separator "*") from aa group by id;
eg:
select * from aa;

select id,group_concat(name) from aa group by id;

select id,group_concat(name separator ';') from aa group by id;
select id,group_concat(distinct name separator ';') from aa group by id;

select id,group_concat(name order by name desc separator "*") from aa group by id;

边栏推荐
- simpleParallax. JS (create poor visual effects for website pictures)
- Configure stylelint
- Record 405 questions about bank callback post request
- [combinatorics] permutation and combination (examples of combinatorial number of multiple sets | three counting models | selection problem | combinatorial problem of multiple sets | nonnegative intege
- 全局事件总线
- JS continues to explore...
- JVM class loading
- Screenshot of the operation steps of upload labs level 4-level 9
- FPGA测试方法以Mentor工具为例
- 3D vision - 2 Introduction to pose estimation - openpose includes installation, compilation and use (single frame, real-time video)
猜你喜欢

Comprehensive case of MySQL data addition, deletion, modification and query

Uio-66-cooh loaded bendamostine | hydroxyapatite (HA) coated MIL-53 (FE) nanoparticles | baicalin loaded manganese based metal organic skeleton material

全局事件总线

Metal organic framework MOFs loaded with non steroidal anti-inflammatory drugs | zif-8 wrapped Prussian blue loaded quercetin (preparation method)
[email protected])|制备路线"/>叶酸修饰的金属-有机骨架(ZIF-8)载黄芩苷|金属有机骨架复合磁性材料([email protected])|制备路线

Why are grass-roots colleges and universities with "soil and poverty" called "Northeast small Tsinghua"?
![Mysql:insert date:sql error [1292] [22001]: data truncation: incorrect date value:](/img/2f/33504391a661ecb63d42d75acf3a37.png)
Mysql:insert date:sql error [1292] [22001]: data truncation: incorrect date value:

Rasp implementation of PHP
![[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update](/img/b8/31609303fd817c48b6fff7c43f31e5.png)
[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update

Solve MySQL 1045 access denied for user 'root' @ 'localhost' (using password: yes)
随机推荐
MySQL 数据增删改查综合案例
The small project (servlet+jsp+mysql+el+jstl) completes a servlet with login function, with the operation of adding, deleting, modifying and querying. Realize login authentication, prevent illegal log
Qt学习21 Qt 中的标准对话框(下)
金属有机骨架MOFs装载非甾体类抗炎药物|ZIF-8包裹普鲁士蓝负载槲皮素(制备方法)
Similarities and differences of sessionstorage, localstorage and cookies
Solve MySQL 1045 access denied for user 'root' @ 'localhost' (using password: yes)
Redis: commandes d'action pour les données de type chaîne
Failure of vector insertion element iterator in STL
核酸修饰的金属有机框架药物载体|PCN-223金属有机骨架包载Ad金刚烷|ZIF-8包裹阿霉素(DOX)
Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock
Common mixins
Uniapp tips - set background music
金属有机骨架MIL-88负载阿霉素DOX|叶酸修饰UiO-66-NH2负载阿霉素[email protected]纳米粒子
selenium 浏览器(1)
金属有机骨架材料ZIF-8包载姜黄素([email protected]纳米颗粒)|纳米金属有机框架搭载雷帕霉素|科研试剂
Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)
Go language unit test 3: go language uses gocovey library to do unit test
28: Chapter 3: develop Passport Service: 11: define attributes in the configuration file, and then obtain them in the code;
Screenshot of the operation steps of upload labs level 4-level 9
Comprehensive case of MySQL data addition, deletion, modification and query