当前位置:网站首页>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');//112233
2:concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL
select concat('11','22',null);//null
3: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;
边栏推荐
- 3D视觉——2.人体姿态估计(Pose Estimation)入门——OpenPose含安装、编译、使用(单帧、实时视频)
- QT learning 17 dialog box and its types
- Uniapp skills - scrolling components -1
- Collection of mobile adaptation related articles
- Windos creates Cordova prompt because running scripts is prohibited on this system
- UiO-66-COOH装载苯达莫司汀|羟基磷灰石( HA) 包裹MIL-53(Fe)纳米粒子|装载黄芩苷锰基金属有机骨架材料
- 1px problem of mobile terminal
- Use vscode to view hex or UTF-8 codes
- [acnoi2022] guess numbers
- JS download files through URL links
猜你喜欢
Redis: redis data structure and key operation commands
金属有机骨架MOFs装载非甾体类抗炎药物|ZIF-8包裹普鲁士蓝负载槲皮素(制备方法)
Mysql:insert date:SQL 错误 [1292] [22001]: Data truncation: Incorrect date value:
JVM family - overview, program counter day1-1
Programmable logic device software testing
Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock
MySQL data processing value addition, deletion and modification
Another industry has been broken by Chinese chips. No wonder the leading analog chip companies in the United States have cut prices and sold off
Why are grass-roots colleges and universities with "soil and poverty" called "Northeast small Tsinghua"?
[email"/>
Folic acid modified metal organic framework (zif-8) baicalin loaded metal organic framework composite magnetic material (AU- [email
随机推荐
[acnoi2022] guess numbers
[combinatorics] permutation and combination (two counting principles, examples of set permutation | examples of set permutation and circle permutation)
Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
How to delete an attribute or method of an object
GoLand 2021.2 configure go (go1.17.6)
QT learning 21 standard dialog box in QT (Part 2)
1px problem of mobile terminal
MySQL data processing value addition, deletion and modification
[technology development-24]: characteristics of existing IOT communication technology
Leetcode-1175. Prime Arrangements
GoLand 2021.1.1: configure the multi line display of the tab of the open file
Common plug-ins for vite project development
[ACNOI2022]猜数
FPGA test method takes mentor tool as an example
Screenshot of the operation steps of upload labs level 4-level 9
PhpMyAdmin stage file contains analysis traceability
JS input number and standard digit number are compared. The problem of adding 0 to 0
Simulated access
Qt学习21 Qt 中的标准对话框(下)
Uio-66-cooh loaded bendamostine | hydroxyapatite (HA) coated MIL-53 (FE) nanoparticles | baicalin loaded manganese based metal organic skeleton material