当前位置:网站首页>group_concat学习与配置
group_concat学习与配置
2022-06-28 12:19:00 【今夜无风亦无雨】
目录
1、背景
在开发过程中,在对数据进行Group By的同时,若想将同一类数据的多个字段以特殊字符串分隔拼装成一个字段返回,此时可以用到MySQL分组拼接函数group_concat.
2、设置
group_concat能够拼接的字符串长度受限于MySQL服务器的配置,默认至允许拼接1024个字节长度的字符串,可以通过如下指令查询:
show variables like 'group_concat_max_len';
执行结果:
注意:当要拼接的长度超过了1024,数据库会自动进行截断,在生产环境中,会出现数据丢失的问题。可以通过如下两种方式解决。
临时设置:弊端当数据库服务重启的时候,如下设置会失效
set session group_concat_max_len = 指定改变的长度;
永久设置:在数据库的my.ini配置文件上加入如下配置
group_concat_max_len = 指定改变的长度
另外,也可以根据如下配置,配置group_concat的最大值
group_concat_max_len = -1
注意:在mysql配置文件中进行了配置更改的,需要重启数据库服务。
3、用例
1)表结构:
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
-- 对class进行分组,将id和name以逗号拼接成一个字段
select GROUP_CONCAT(DISTINCT id,',',name order by sorce desc separator '|') as msg
from stu
group by class;
边栏推荐
- Bisection (integer bisection and floating point bisection)
- PyQt5可视化开发
- 设置Canvas的 overrideSorting不生效
- [unity Editor Extension practice] find all prefabs referencing this picture
- 内部振荡器、无源晶振、有源晶振有什么区别?
- 【Unity编辑器扩展实践】、利用txt模板动态生成UI代码
- 不到一小时,苹果摧毁了15家初创公司
- .NET混合开发解决方案24 WebView2对比CefSharp的超强优势
- 搭建学习环境
- Three ways to implement LRU cache (recommended Collection)
猜你喜欢
什么是数据合规?怎样做到数据合规?
Redis 原理 - List
攻防世界新手入门hello_pwn
Unity Editor Extension Foundation, editorguilayout (II)
Android应用安全之JNI混淆
RemoteViews的作用及原理
Ugui force refresh of layout components
[C language] about scanf() and scanf_ Some problems of s()
RemoteViews布局和类型限制源码分析
EMC RS485 interface EMC circuit design scheme
随机推荐
cdc同步 如果数据库表的主键发生了变化,会同步成两个数据 还是会同步更新主键呢?
[C language] about scanf() and scanf_ Some problems of s()
【C语言】文件读写函数使用
JNI函数的2种书写方式
Custom title bar view
AcWing 605. Simple product (implemented in C language)
深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图...
杰理之wif 干扰蓝牙【篇】
2022招商FinTech比赛总结
杰理之wif 干扰蓝牙【篇】
攻防世界新手入门hello_pwn
杰理之wif 干扰蓝牙【篇】
最新!基于Open3D的点云处理入门与实战教程
开源项目维权成功案例: spug 开源运维平台成功维权
[C language] use of nested secondary pointer of structure
Ugui force refresh of layout components
What are the common modes of financial products in 2022?
Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting
为什么CAD导出PDF没有颜色
Url追加参数方法,考虑#、?、$的情况