当前位置:网站首页>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
边栏推荐
猜你喜欢
![[C language] dynamic memory management, flexible array](/img/da/b9455885df0cb6646908e3655d62c5.png)
[C language] dynamic memory management, flexible array

Ten thousand words long, one word thoroughly! Finally, someone has made business intelligence (BI) clear

Shell脚本

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

nacos2.1.0集群搭建

Silicon Valley class lesson 11 - official account news and wechat authorization

activemq--消息重试机制

『每日一问』ReentrantLock加锁解锁

Opencv realizes simple face tracking

SSM框架整合,简单案例
随机推荐
activemq--可持久化机制之JDBC
无法再web服务器上启动调试,web服务器未能找到请求资源
Detailed explanation of pipeline pipeline mechanism in redis
ActiveMQ -- persistent mechanism
[stl]list Simulation Implementation
『每日一问』ReentrantLock加锁解锁
Six storage types in C language: Auto register static extern const volatile
jsPDF生成PDF文件,文件不全问题,后台进行文件下载,前台不下载
什么是贫血模型和充血模型?
uni-app - Refused to display ‘xxx‘ in a frame because an ancestor violates the following Content Sec
微信小程序中的列点击隐藏,再点击显示
What are the commands used by pl/sql tools to export SQL files?
Software examination system architecture designer concise tutorial | software life cycle
Disable module (attribute node) in LabVIEW
动态添加多tab,并初始化每个tab页面
Neural network learning (1) Introduction
Shell script
The development of art NFT
[arm] Xintang nuc977 transplants wk2124 drive
Composition of the interview must ask items