当前位置:网站首页>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;
边栏推荐
- How to delete an attribute or method of an object
- QT learning 23 layout manager (II)
- FPGA test method takes mentor tool as an example
- Redis:字符串類型數據的操作命令
- Dynamic programming 01 knapsack and complete knapsack
- Cross linked cyclodextrin metal organic framework loaded methotrexate slow-release particles | metal organic porous material uio-66 loaded with flavonoid glycosides | Qiyue
- Duet date picker (time plug-in that can manually enter the date)
- Generate directories from web content
- Mysql:insert date:SQL 错误 [1292] [22001]: Data truncation: Incorrect date value:
- 3D vision - 2 Introduction to pose estimation - openpose includes installation, compilation and use (single frame, real-time video)
猜你喜欢
Mysql:insert date:sql error [1292] [22001]: data truncation: incorrect date value:
Go language web development series 30: gin: grouping by version for routing
28: Chapter 3: develop Passport Service: 11: define attributes in the configuration file, and then obtain them in the code;
Redis: commandes d'action pour les données de type chaîne
RocksDB LRUCache
JVM系列——概述,程序计数器day1-1
Go language unit test 4: go language uses gomonkey to test functions or methods
【吉林大学】考研初试复试资料分享
Redis: redis data structure and key operation commands
金属有机骨架MOFs装载非甾体类抗炎药物|ZIF-8包裹普鲁士蓝负载槲皮素(制备方法)
随机推荐
QT learning 17 dialog box and its types
解决MySql 1045 Access denied for user ‘root‘@‘localhost‘ (using password: YES)
28:第三章:开发通行证服务:11:在配置文件中定义属性,然后在代码中去获取;
Metal organic framework (MOFs) antitumor drug carrier | pcn-223 loaded with metronidazole | uio-66 loaded with ciprofloxacin hydrochloride(
Similarities and differences of sessionstorage, localstorage and cookies
[ACNOI2022]猜数
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
Windos creates Cordova prompt because running scripts is prohibited on this system
Uniapp tips - scrolling components
[技術發展-24]:現有物聯網通信技術特點
Current situation, analysis and prediction of information and innovation industry
Qt学习17 对话框及其类型
Configure stylelint
Golang - command line tool Cobra
jvm-对象生命周期
金属有机骨架材料ZIF-8包载姜黄素([email protected]纳米颗粒)|纳米金属有机框架搭载雷帕霉素|科研试剂
simpleParallax. JS (create poor visual effects for website pictures)
QT learning 24 layout manager (III)
MySQL 数据处理值增删改
Example analysis of QT learning 18 login dialog box