MySQL function: use of row-to-column CONCAT, CONCAT_WS, GROUP_CONCAT (essential)

foreword

I haven't touched Mysql for a long time .

On a whim today, I suddenly wanted to know about Mysql column switching. After reading some articles, the focus was not clear, so I have the following!

Mysql official websitecommunity version downloadWindows version_mysql.8.0.31 download   )

overview

Mysql internally provides three functions for column conversion, namely:

CONCAT(str1,str2,...)

CONCAT_WS(separator,str1,str2,...)

GROUP_CONCAT(expr)

data preparation

The data is as follows, simple and clear.

#1、查看原始数据
select * from student;  

 

As shown in the figure above, 3 pieces of data are enough to explain the problem. 

case analysis

1. Analysis of CONCAT( str1,str2,... ) function

First of all, when I saw the parameters of this function, str1 and str2, I immediately had an impulse. These two parameters should be regarded as arbitrary strings.

1. Use the concat function to concatenate any string

select CONCAT('aaa',',','bbb') as info from dual;

 

 The effect is shown in the figure: concat can be used to splice any string and customize the separator !

2. Use the concat function to put each row of data into multiple columns --> one row 

select CONCAT(s.id,',',s.name,',',s.age) as infos from student s;

3. Precautions

If you just want to convert the single-column data in the table into one row, the concat function will seem a bit powerless, and you need to use the group_concat function, which will be introduced in the third section below ! 

2. Analysis of CONCAT_WS( separator,str1,str2,... ) function

From the grammar, the separator is the delimiter , followed by str1 and str2, etc., are spliced ​​fields.

It is not difficult to understand that this function only needs to specify the delimiter once

select CONCAT_WS('#',id,name,age )  as infos from student  ;

Note: It is a pity that the CONCAT_WS function still cannot allow a single column to switch!

There are specializations in the technical industry, and the following GROUP_CONCAT function is required for a single case to switch careers!

3. Analysis of GROUP_CONCAT( expr ) function ( emphasis )

1. Single case transfer

select GROUP_CONCAT(name) as names from student ;

 

Explanation: This case is a simple demonstration of this function, and conditional statements such as where group by can still be added after the SQL. 

2. Expansion

select GROUP_CONCAT('111',name) as names from student ;
select GROUP_CONCAT(name,'222') as names from student ;
select GROUP_CONCAT('111',name,'222') as names from student ;

operation result:

111succ,111chouniu,111succ
succ222,chouniu222,succ222
111succ222,111chouniu222,111succ222

4. Expansion

Oracle commonly used column conversion

select * from emp;
--方法一:使用concat函数,该函数只能有两个参数(不可以自定义分隔符),但是它允许套用
select concat(empno,ename) from emp; 
SELECT CONCAT(CONCAT('A', 'B'),'C')FROM dual;

--可以合并任意多个列为一行
select empno || ',' ||ename from emp;

--单列合并为一行,默认分隔符为逗号,如果想制定分隔符,则需要用replace函数协助
select wm_concat(ename) from emp;    
select replace(wm_concat(ename),',','|') from emp;

Summarize

So far, the use of the three functions CONCAT , CONCAT_WS , and GROUP_CONCAT has been basically introduced clearly. Of course, in actual development, the scene will be much more complicated.

The good news is that with the assistance of these three functions, it will be even more powerful to realize more complicated row-to-column conversion in the later stage!

Epilogue

There are too many MySql knowledge points, and more essential mysql knowledge points will be released later, if they are not released, they will be fine products , no nonsense!

If you find it useful, please leave a message, like, and bookmark!

Guess you like

Origin blog.csdn.net/xp871038951/article/details/128266346