当前位置:网站首页>Analysis of concat and group in MySQL_ Use of concat
Analysis of concat and group in MySQL_ Use of concat
2022-07-25 09:21:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
The examples used in this article are in the following database tables tt2 perform :
One 、concat() function
1、 function : Concatenate multiple strings into a single string .
2、 grammar :concat(str1, str2,…)
The return result is the string generated by the connection parameter , If any of the parameters is null, The return value is null.
3、 give an example :
example 1:
select concat (id, name, score) as info from tt2;There is an act in the middle null Because tt2 There is a row in the table score The value is null.
example 2: In case 1 Three fields in the result of id,name,score The combination of does not have a delimiter , We can add a comma as a separator :
This seems to be pleasing to the eye a lot ~~
But input sql Statements are a lot of trouble , You need to enter commas twice for the three fields , If 10 A field , To enter a comma nine times … The trouble is dead , Is there any easy way ?—— You can then specify the separator between parameters concat_ws() coming !!!
Two 、concat_ws() function
1、 function : and concat() equally , Concatenate multiple strings into a single string , But you can specify the separator all at once ~(concat_ws Namely concat with separator)
2、 grammar :concat_ws(separator, str1, str2, …)
explain : The first parameter specifies the separator . It should be noted that the separator cannot be null, If null, Then the returned result is null.
3、 give an example :
example 3: We use concat_ws() take The separator is specified as a comma , Reach and example 2 Same effect :
example 4: Specify the separator as null, It all turned out to be null:
3、 ... and 、group_concat() function
Preface : There is group by In the query statement of ,select The specified field is either contained in the group by After statement , As a basis for grouping , Or in aggregate functions .
example 5:
This example queries name The smallest of the same people id. If we want to check name All of the same people id Well ?
Of course, we can query like this :
example 6:
But the same name appears many times , It doesn't look intuitive . Is there a more intuitive way , Let each name appear only once , It can show all the people with the same name id Well ?—— Use group_concat()
1、 function : take group by The resulting values in the same group are connected , Returns a string result .
2、 grammar :group_concat( [distinct] Fields to connect [order by Sort field asc/desc ] [separator ‘ Separator ’] )
explain : By using distinct You can exclude duplicate values ; If you want to sort the values in the results , have access to order by Clause ;separator Is a string value , The default is a comma .
3、 give an example :
example 7: Use group_concat() and group by Show people with the same name id Number :
example 8: The above id The numbers are sorted from large to small , And use ’_’ As a separator :
example 9: The query above shows that name All in each group in the group id. Next, we will query to name Of all groups grouped id and score:
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/111434.html Link to the original text :https://javaforall.cn
边栏推荐
- Rich text style word image processing
- 抽象类和接口的区别(最详细)
- 什么是贫血模型和充血模型?
- How to write the code of wechat applet implementation tab
- Silicon Valley classroom lesson 12 - official account on demand course and live broadcast management module
- Comparison between symmetric encryption and asymmetric encryption
- Activemq-- asynchronous delivery
- Dark horse programmer JDBC
- Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
- yarn : 无法加载文件 yarn.ps1,因为在此系统上禁止运行脚本。
猜你喜欢

Bi business interview with data center and business intelligence (I): preparation for Industry and business research

The garbage classification data set used in the excellent Yolo target detection training is shared - about 3000 labeled

NFT guide for musicians

ActiveMQ -- dead letter queue

什么是单机、集群与分布式?
![[deep learning] overview | the latest progress of deep learning](/img/b9/6117862397dcda4d555c819e913c9b.png)
[deep learning] overview | the latest progress of deep learning

uni-app - Refused to display ‘xxx‘ in a frame because an ancestor violates the following Content Sec

Ranking of data results in MySQL

整理 华为AP-3010DN_V2配置创建wifi

OpenCV实现简单的人脸追踪
随机推荐
yarn : 无法加载文件 yarn.ps1,因为在此系统上禁止运行脚本。
分享一个避免递归的部门设计方法
Activemq-- delayed delivery and scheduled delivery
How to use pixi.js to make simple Parkour games
idea中将lib目录下的jar包加入到项目中
Notes on in-depth analysis of C language 2
深入理解static关键字
Composition of the interview must ask items
ActiveMQ -- message retry mechanism
Nacos启动报错Unable to start web server
Activemq-- asynchronous delivery
神经网络学习(1)前言介绍
How to realize the drop-down option box of wechat applet
NFT guide for musicians
[stl]list Simulation Implementation
Feiling ok1028a core board adapts to rtl8192cu WiFi module
What are the commands used by pl/sql tools to export SQL files?
Thymeleaf 笔记
Do you know these methods of MySQL database optimization?
Dark horse programmer JDBC