博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Group_Concat函数示例
阅读量:5060 次
发布时间:2019-06-12

本文共 3627 字,大约阅读时间需要 12 分钟。

1. 函数定义:

GROUP_CONCAT([DISTINCT] expr [,expr ...]             [ORDER BY {unsigned_integer | col_name | expr}                 [ASC | DESC] [,col_name ...]]             [SEPARATOR str_val])

2. 创建测试数据:

CREATE TABLE IF NOT EXISTS `departmentweekdata` (  `department` varchar(255) NOT NULL,  `week` varchar(10) NOT NULL,  `interval` tinyint(4) DEFAULT NULL,  `number` int(11) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;---- 插入数据 `departmentweekdata`--INSERT INTO `departmentweekdata` (`department`, `week`, `interval`, `number`) VALUES('CAO', '2014-12', 1, 1),('GBG1', '2014-12', 1, 0),('CAO', '2014-12', 2, 0),('GBG1', '2014-12', 2, 0),('CAO', '2014-12', 3, 1),('GBG1', '2014-12', 3, 0),('CAO', '2014-11', 1, 2),('GBG1', '2014-11', 1, 0),('CAO', '2014-11', 2, 0),('GBG1', '2014-11', 2, 0),('CAO', '2014-11', 3, 1),('GBG1', '2014-11', 3, 0),('CAO', '2014-10', 1, 1),('GBG1', '2014-10', 1, 0),('CAO', '2014-10', 2, 2),('GBG1', '2014-10', 2, 0),('CAO', '2014-10', 3, 0),('GBG1', '2014-10', 3, 0),('CAO', '2014-09', 1, 1),('GBG1', '2014-09', 1, 0),('CAO', '2014-09', 2, 0),('GBG1', '2014-09', 2, 0),('CAO', '2014-09', 3, 0),('GBG1', '2014-09', 3, 0),('CAO', '2014-08', 1, 2),('GBG1', '2014-08', 1, 0),('CAO', '2014-08', 2, 0),('GBG1', '2014-08', 2, 0),('CAO', '2014-08', 3, 1),('GBG1', '2014-08', 3, 0),('CAO', '2014-07', 1, 1),('GBG1', '2014-07', 1, 0),('CAO', '2014-07', 2, 2),('GBG1', '2014-07', 2, 0),('CAO', '2014-07', 3, 0),('GBG1', '2014-07', 3, 0),('CAO', '2014-06', 1, 0),('GBG1', '2014-06', 1, 0),('CAO', '2014-06', 2, 1),('GBG1', '2014-06', 2, 0),('CAO', '2014-06', 3, 0),('GBG1', '2014-06', 3, 0),('CAO', '2014-05', 1, 0),('GBG1', '2014-05', 1, 0),('CAO', '2014-05', 2, 1),('GBG1', '2014-05', 2, 0),('CAO', '2014-05', 3, 0),('GBG1', '2014-05', 3, 0),('CAO', '2014-04', 1, 1),('GBG1', '2014-04', 1, 0),('CAO', '2014-04', 2, 0),('GBG1', '2014-04', 2, 0),('CAO', '2014-04', 3, 0),('GBG1', '2014-04', 3, 0),('CAO', '2014-03', 1, 0),('GBG1', '2014-03', 1, 0),('CAO', '2014-03', 2, 0),('GBG1', '2014-03', 2, 0),('CAO', '2014-03', 3, 1),('GBG1', '2014-03', 3, 0);

2. 根据部门,间隔;将数量列组合成一个字符串;

SELECT Temp.`department`,`Temp`.interval,group_concat(number) AS TenWeekStringFROM `weekdata` TempGROUP BY Temp.`department`,Temp.`interval`;

查询结果:

CAO 1 1,1,1,0,1,2,2,0,0,1
CAO 2 0,0,1,1,2,0,2,0,0,0
CAO 3 1,1,0,0,0,1,0,0,1,0
GBG1 1 0,0,0,0,0,0,0,0,0,0
GBG1 2 0,0,0,0,0,0,0,0,0,0
GBG1 3 0,0,0,0,0,0,0,0,0,0

 

 

 

 

 

 

3. 同样的查询, SEPARATOR设定字符串的分隔符:

SELECT     Temp.`department`    ,`Temp`.interval    ,Group_Concat(number SEPARATOR '|') AS TenWeekStringFROM `weekdata` TempGROUP BY Temp.`department`,Temp.`interval`;

查询结果:

CAO 1 1|1|1|0|1|2|2|0|0|1
CAO 2 0|0|1|1|2|0|2|0|0|0
CAO 3 1|1|0|0|0|1|0|0|1|0
GBG1 1 0|0|0|0|0|0|0|0|0|0
GBG1 2 0|0|0|0|0|0|0|0|0|0
GBG1 3 0|0|0|0|0|0|0|0|0|0

 

 

 

 

 

 

4.获得经排序的字符串:

SELECT     Temp.`department`    ,`Temp`.interval    ,Group_Concat(number ORDER BY `temp`.`week` ASC SEPARATOR '|') AS TenWeekStringFROM `weekdata` TempGROUP BY Temp.`department`,Temp.`interval`;

查询结果:

CAO 1 0|1|0|0|1|2|1|1|2|1
CAO 2 0|0|1|1|2|0|0|2|0|0
CAO 3 1|0|0|0|0|1|0|0|1|1
GBG1 1 0|0|0|0|0|0|0|0|0|0
GBG1 2 0|0|0|0|0|0|0|0|0|0
GBG1 3 0|0|0|0|0|0|0|0|0|0

 

 

 

 

 

 

5.去除重复的值:

SELECT     Temp.`department`    ,`Temp`.interval    ,Group_Concat(DISTINCT `number` ORDER BY `temp`.`week` ASC SEPARATOR '|') AS TenWeekStringFROM `weekdata` TempGROUP BY Temp.`department`,Temp.`interval`;

查询结果:

CAO 1 0|2|1
CAO 2 1|2|0
CAO 3 1|0
GBG1 1 0
GBG1 2 0
GBG1 3 0

 

 

 

 

 

 

6.设定输出字符串的最大长度:

 查看最大长度:

SELECT @@global.group_concat_max_len as `max_length`;

查询结果:

1024

 

 设定最大长度(最大值不能超过4294967295):

SET GLOBAL group_concat_max_len=1024000000;

转载于:https://www.cnblogs.com/ucos/p/3631223.html

你可能感兴趣的文章
144 Binary Tree Preorder Travesal
查看>>
Linux信号实践(5) --时间与定时器
查看>>
题---
查看>>
Linux常用命令
查看>>
20151213调转页面以及页面传值
查看>>
你的密码是什么?
查看>>
五子棋Web版的开发(一)---搭建IDEA SSH环境
查看>>
hdu 1159 Common Subsequence(最长公共子序列 DP)
查看>>
POJ 2251 Dungeon Master
查看>>
重要道路
查看>>
【题解】 bzoj3894: 文理分科 (网络流/最小割)
查看>>
linux——nano
查看>>
How to modify rosbag?如何修改rosbag?
查看>>
2012 GCR MVP Openday 手记
查看>>
NOIP2017D2T1 奶酪 洛谷P3958
查看>>
51nod 1486 大大走格子
查看>>
好书推荐之《人类群星闪耀时》 隐私策略(Privacy policy)
查看>>
Oracle 行拼接 wmsys.wm_concat扩展
查看>>
Dexdump 无法正常反编译问题
查看>>
栈的压入、弹出顺序
查看>>