当前位置:网站首页>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 = -1
Be 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;
边栏推荐
- 杰理之SPI1外挂FLASH录音修改【篇】
- 【编解码】从零开始写H264解码器(1) 总纲
- Unity加载设置:Application.backgroundLoadingPriority
- Jerry's wif interferes with Bluetooth [chapter]
- 内部振荡器、无源晶振、有源晶振有什么区别?
- Custom title bar view
- 杰理之wif 干扰蓝牙【篇】
- 最新汇总!30省份公布2022高考分数线
- What is the difference between internal oscillator, passive crystal oscillator and active crystal oscillator?
- Wechat authorized login
猜你喜欢
Login interface accesses and clears the token
【C语言】判断三角形
ByteV搭建动态数字孪生网络安全平台----助力网络安全发展
Given two points and a point with a middle scale, find the coordinates of the point
Android应用安全之JNI混淆
Ugui force refresh of layout components
group_concat学习与配置
Unity Editor Extension Foundation, editorguilayout (III)
RemoteViews的作用及原理
智联招聘基于 Nebula Graph 的推荐实践分享
随机推荐
[unity Editor Extension Foundation], editorguilayout (I)
【C语言】文件读写函数使用
ASP.NET CORE Study06
杰理之wif 干扰蓝牙【篇】
如何在Microsoft Exchange 2010中安装SSL证书
【C语言】判断三角形
ASP.NET CORE Study04
【编解码】从零开始写H264解码器(1) 总纲
[C language] use of file read / write function
Tips for using ugui (V) using scroll rect component
杰理之wif 干扰蓝牙【篇】
Map sorting tool class
我的NVIDIA开发者之旅-Jetson Nano 2gb教你怎么训练模型(完整的模型训练套路)
Unity Editor Extension Foundation, GUI
websocket 1 分钟自动断开连接
Wechat authorized login
Beginner level of attack and defense World Hello_ pwn
Truly understand triode beginner level chapter (Classic) "suggestions collection"
UGUI使用小技巧(五) Scroll Rect组件的使用
Jerry's wif interferes with Bluetooth [chapter]