当前位置:网站首页>group_ Concat learning and configuration
group_ Concat learning and configuration
2022-06-28 12:29:00 【No wind or rain tonight】
Catalog
1、 background
In the development process , On the data Group By At the same time , If you want to assemble multiple fields of the same type of data into a field separated by a special string, it returns , You can use MySQL Group splicing function group_concat.
2、 Set up
group_concat The length of strings that can be spliced is limited to MySQL Server configuration , Default to allow splicing 1024 Byte length string , You can query through the following instructions :
show variables like 'group_concat_max_len';Execution results :

Be careful : When the length to be spliced exceeds 1024, The database will be truncated automatically , In the production environment , There will be data loss . It can be solved in two ways .
Temporary settings : Disadvantages: when the database service is restarted , The following settings will fail
set session group_concat_max_len = Specify the length of the change ;Permanent settings : In the database my.ini Add the following configuration to the configuration file
group_concat_max_len = Specify the length of the change in addition , It can also be configured as follows , To configure group_concat The maximum of
group_concat_max_len = -1Be careful : stay mysql Configuration file with configuration changes , The database service needs to be restarted .
3、 Use cases
1) Table structure :
CREATE TABLE `stu` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`class` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`sorce` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 321 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;2)SQL
-- Yes class Grouping , take id and name Concatenate a comma into a field
select GROUP_CONCAT(DISTINCT id,',',name order by sorce desc separator '|') as msg
from stu
group by class;
边栏推荐
猜你喜欢

Custom title bar view

Function and principle of remoteviews
Three ways to implement LRU cache (recommended Collection)

. Net hybrid development solution 24 webview2's superior advantages over cefsharp

Beginner level of attack and defense World Hello_ pwn

My NVIDIA developer tour -jetson nano 2GB teaches you how to train models (complete model training routines)

结构光之相移法+多频外差的数学原理推导

RemoteViews的作用及原理

深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图...

最新汇总!30省份公布2022高考分数线
随机推荐
[C language] about scanf() and scanf_ Some problems of s()
2022招商FinTech比赛总结
搭建学习环境
杰理之SPI1外挂FLASH录音修改【篇】
C语言 sprintf函数使用详解
2022年理财产品的常见模式有哪些?
Setting overridesorting for canvas does not take effect
洛谷_P1303 A*B Problem_高精度计算
group_concat学习与配置
微信授权登陆
Unity Editor Extension Foundation, guilayout
ASP.NET CORE Study04
【C语言】如何很好的实现复数类型
Sha256 encryption tool class
[unity Editor Extension practice], find all prefabrications through code
【Unity编辑器扩展实践】、通过代码查找所有预制
深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图...
What are the common modes of financial products in 2022?
【C语言】二叉树的实现及三种遍历
Zero basic C language (I)