当前位置:网站首页>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;

边栏推荐
- Golang — template
- Common network state detection and analysis tools
- Common plug-ins for vite project development
- QT learning 22 layout manager (I)
- Collection of mobile adaptation related articles
- Webpage connection database ~ simple implementation of addition, deletion, modification and query complete code
- QT learning 20 standard dialog box in QT (middle)
- Qt学习22 布局管理器(一)
- 信创产业现状、分析与预测
- JVM系列——概述,程序计数器day1-1
猜你喜欢

Use and design of Muduo buffer class

Dlopen() implements dynamic loading of third-party libraries

Page generation QR code

How to use lxml to judge whether the website announcement is updated
[email protected])"/>金属有机骨架(MOFs)抗肿瘤药载体|PCN-223装载甲硝唑|UiO-66包载盐酸环丙沙星([email protected])

Vite project commissioning

UiO-66-COOH装载苯达莫司汀|羟基磷灰石( HA) 包裹MIL-53(Fe)纳米粒子|装载黄芩苷锰基金属有机骨架材料

3D视觉——2.人体姿态估计(Pose Estimation)入门——OpenPose含安装、编译、使用(单帧、实时视频)

QT learning 19 standard dialog box in QT (top)

MySQL data processing value addition, deletion and modification
随机推荐
金属有机骨架MIL-88负载阿霉素DOX|叶酸修饰UiO-66-NH2负载阿霉素[email protected]纳米粒子
Go 1.16.4: manage third-party libraries with Mod
Qt学习24 布局管理器(三)
JVM系列——概述,程序计数器day1-1
Conversion function and explicit
Print. JS -- web page file printing
simpleParallax. JS (create poor visual effects for website pictures)
Scroll detection of the navigation bar enables the navigation bar to slide and fix with no content
从零开始的基于百度大脑EasyData的多人协同数据标注
Article content typesetting and code highlighting
PhpMyAdmin stage file contains analysis traceability
GoLand 2021.2 configure go (go1.17.6)
消息订阅与发布
jvm-运行时数据区
SQL Injection (AJAX/JSON/jQuery)
Ocean CMS vulnerability - search php
Common network state detection and analysis tools
Interface for querying IP home
[ACNOI2022]猜数
Thrift threadmanager and three monitors