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

边栏推荐
- Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)
- [combinatorics] permutation and combination (examples of combinatorial number of multiple sets | three counting models | selection problem | combinatorial problem of multiple sets | nonnegative intege
- [technology development-24]: characteristics of existing IOT communication technology
- 如何使用lxml判断网站公告是否更新
- [combinatorics] permutation and combination (two counting principles, examples of set permutation | examples of set permutation and circle permutation)
- Cross linked cyclodextrin metal organic framework loaded methotrexate slow-release particles | metal organic porous material uio-66 loaded with flavonoid glycosides | Qiyue
- Message subscription and publishing
- 叶酸修饰的金属-有机骨架(ZIF-8)载黄芩苷|金属有机骨架复合磁性材料([email protected])|制备路线
- Qt学习21 Qt 中的标准对话框(下)
- JS new challenges
猜你喜欢

GoLand 2021.1: rename the go project

FPGA测试方法以Mentor工具为例

Qt学习24 布局管理器(三)

Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock

JS Part III

GoLand 2021.1.1: configure the multi line display of the tab of the open file

Global event bus

PhpMyAdmin stage file contains analysis traceability

Scroll detection of the navigation bar enables the navigation bar to slide and fix with no content

JVM系列——概述,程序计数器day1-1
随机推荐
JS download files through URL links
Leetcode-1175. Prime Arrangements
SQL Injection (AJAX/JSON/jQuery)
QT learning 20 standard dialog box in QT (middle)
Ocean CMS vulnerability - search php
Spring cup eight school league
28: Chapter 3: develop Passport Service: 11: define attributes in the configuration file, and then obtain them in the code;
Metal organic framework MOFs loaded with non steroidal anti-inflammatory drugs | zif-8 wrapped Prussian blue loaded quercetin (preparation method)
MySQL data processing value addition, deletion and modification
Interface for querying IP home
叶酸修饰的金属-有机骨架(ZIF-8)载黄芩苷|金属有机骨架复合磁性材料([email protected])|制备路线
How to delete an attribute or method of an object
Uniapp skills - scrolling components -1
从零开始的基于百度大脑EasyData的多人协同数据标注
[acnoi2022] guess numbers
Why are grass-roots colleges and universities with "soil and poverty" called "Northeast small Tsinghua"?
Duet date picker (time plug-in that can manually enter the date)
JS general form submission 1-onsubmit
GoLand 2021.2 configure go (go1.17.6)
page owner特性浅析