MySQL-- string concatenation function concat, concat_ws and magic string functions group_concat

MySQL-- string concatenation function concat, concat_ws and magic string functions group_concat


      For more information about the database, please add attention yo ~ ~. For bloggers please contact Gabor main private letter or contact:
      QQ: 3327908431
      micro letter: ZDSL1542334210

        Introduction: MySQL magic string concatenation function **

1, concat syntax and concat_ws

        As the name suggests, concat is the connection means is stitching together the two strings, the same concat_ws also concatenate strings, but it can specify what character spliced ​​with the syntax is:

concat(<字符串1>,<字符串2>,<字符串3>,...);
concat_ws('-',<字符串1>,<字符串2>,<字符串3>,...); #以'-'作为拼接的字符

2, group_concat grammar

        group_concat function returns a string result, which is connected by a value in the packet combination, of course, but it also can function-concat complete, but it is the use of the specified delimiter Separator.

group_concat(<要连接的字段> <Order by asc/desc排序字段> <Separator '分隔符'>)

3, create a table

Create scores of students scoring table

create table scores (s_id varchar(5),
    c_id varchar(3),score float);
    
insert into scores values 
("001","01",135),
("005","01",120),
("003","01",110),
("002","01",90),
("005","02",140),
("001","02",125.5),
("004","02",100),
("006","02",90),
("002","03",102),
("005","03",100.6),
("001","03",100),
("003","03",95.6),
("004","03",83),
("003","02",80),
("006","03",79.5);

4, section title

Topic one: the string 'Hello', '! MySQL' stitching together

select concat('Hello ','MySQL!');   #法一
select group_concat('Hello ','MySQL!');   #法二
select group_concat('Hello ','MySQL!' separator '-');   #法三
# 以上三个答案都是:
Hello MySQL!

Title II: string 'Hello', '! MySQL' stitching together, with the ~ as delimiter

select concat_ws('~','Hello','MySQL!');
# 答案为:
Hello~MySQL!

Topic three: Find scores table, not the course number classmate and connect them

select group_concat(c_id separator '-') from scores;
# 答案:
01-01-01-01-02-02-02-02-03-03-03-03-03-02-03

Topic Four: Find a course number each student's scores from the table and connect them in descending order

select group_concat(c_id order by c_id desc separator '-') from scores;
# 答案:
03-03-03-03-03-03-02-02-02-02-02-01-01-01-01

Topic Five: Find courses and 001 students to learn exactly the same student information

select * from stu left join scores on stu.s_id=scores.s_id  
group by stu.s_id having group_concat(c_id order by c_id) =
(select group_concat(c_id order by c_id) from scores where s_id=001); # 法二

select * from stu where s_id in(select s_id from 
(select s_id,group_concat(c_id order by c_id)a1 from scores group by s_id)aa,
(select group_concat(c_id order by c_id)a2 from scores where s_id=001)bb where aa.a1=bb.a2);    # 法二
# 答案:
001	李华	男	23	1996-8-16	001	02	125.5
003	赵敏	女	23	1990-5-26	003	01	110
005	朱亚军	男	25	1999-8-16	005	01	120
# 法二难度较高,利用多重子查询,供大家参考,若不理解的伙伴可关注我后私信博主。

        Summary: Why group_concat is a magical function? Because if we want to create a field in the list t1 in MySQL, command, alter table t1 add k1, add k2. When you add two fields can be seen better, but if it add 10? 100 it? 1000 do? So this time, we need to use stored procedures and functions group_concat batch generate multiple fields we need, please stay tuned to the specific content.

5, the end of the paper eggs - easy moment

        I do not know if you have not read the news, this is not just a few days ago Well, say Chinese language is profound on the news, to be honest I very much agree with this statement, because my friend had a similar embarrassment to bear it happened, a friend will He asked me: "what matter it?" in fact, I do not want to answer but I do ask everyone to bring some of my friends the story of Winnie, ha ha ha. Well this is not yesterday, National Day and then I bear and his girlfriend take the subway together, his wife discovered he had been playing with it the phone, and he wife said: "Brother Bear, into a beautiful wife sitting next to you , you have been playing phone this really okay "result Cubs answer made me dumbfounded audience, he had this to say:"? I just think playing his wife in a public place it is not appropriate ... "then he said, Well the end, all the people on the subway laugh ha ha ha ha ... and then I do it in order to alleviate the embarrassment of the atmosphere Jizhongshengzhi wife and I wanted him to play a game, then I said: 'sister-in-law, do not be afraid, we play two together ... " Well is such a case.

       Today to end here yo // each article has the end the egg - relaxed moment yo ~ plus interest to learn more about MySQL knowledge! Thank you for watching, I was Jetuser-data

Links: [https://blog.csdn.net/L1542334210]
CSND: L1542334210
Here Insert Picture Description
I wish you all success! Family fun!

Published 29 original articles · won praise 53 · views 30000 +

Guess you like

Origin blog.csdn.net/L1542334210/article/details/102166659