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

边栏推荐
- 使用vscode查看Hex或UTF-8编码
- Metal organic framework MOFs loaded with non steroidal anti-inflammatory drugs | zif-8 wrapped Prussian blue loaded quercetin (preparation method)
- Uniapp skills - dom display and hiding
- [bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update
- Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
- Collection of mobile adaptation related articles
- Use docker to build sqli lab environment and upload labs environment, and the operation steps are provided with screenshots.
- js . Find the first palindrome string in the array
- 【吉林大学】考研初试复试资料分享
- 可编程逻辑器件软件测试
猜你喜欢
![Mysql:insert date:SQL 错误 [1292] [22001]: Data truncation: Incorrect date value:](/img/2f/33504391a661ecb63d42d75acf3a37.png)
Mysql:insert date:SQL 错误 [1292] [22001]: Data truncation: Incorrect date value:

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

JS Part 2

FPGA测试方法以Mentor工具为例

JVM runtime data area

RocksDB LRUCache

Redis:字符串類型數據的操作命令

JS matrix zero

Article content typesetting and code highlighting

Screenshot of the operation steps of upload labs level 4-level 9
随机推荐
Qt学习20 Qt 中的标准对话框(中)
Implementation of Muduo asynchronous logging
从零开始的基于百度大脑EasyData的多人协同数据标注
28:第三章:开发通行证服务:11:在配置文件中定义属性,然后在代码中去获取;
[技術發展-24]:現有物聯網通信技術特點
[développement technologique - 24]: caractéristiques des technologies de communication Internet des objets existantes
Interface for querying IP home
Go 1.16.4: purpose of go mod tidy
C language standard IO function sorting
Programmable logic device software testing
Global event bus
Go language unit test 4: go language uses gomonkey to test functions or methods
Invalid Z-index problem
Configure stylelint
Page generation QR code
JS Part III
Redis: operation command of string type data
Uniapp tips - set background music
Go language web development series 27: Gin framework: using gin swagger to implement interface documents
Scroll detection of the navigation bar enables the navigation bar to slide and fix with no content