group_concat_max_len设置过小致group_concat()排列不全

问题描述:group_concat_max_len默认值1024导致group_concat函数排列显示不足

 

 

 

group_concat_max_len长度显示跟group_concat排列长度息息相关,如果group_concat显示长度超过1024,突破默认值,会出现显示不全的问题

group_concat函数:

mysql> select * from sc;
+-----------+-----+-------+
| Sno       | Cno | Grade |
+-----------+-----+-------+
| 201215121 | 1   |    92 |
| 201215121 | 2   |    85 |
| 201215121 | 3   |    88 |
| 201215122 | 2   |    90 |
| 201215122 | 3   |    80 |
+-----------+-----+-------+
5 rows in set (0.01 sec)

--将Cno横排显示
mysql> select Sno,group_concat(Cno) from sc group by Sno;
+-----------+-------------------+
| Sno       | group_concat(Cno) |
+-----------+-------------------+
| 201215121 | 1,2,3             |
| 201215122 | 2,3               |
+-----------+-------------------+
2 rows in set (0.00 sec)

--将Cno横排显示以''分隔
mysql> select Sno,group_concat(Cno separator ';') from sc group by Sno;
+-----------+---------------------------------+
| Sno       | group_concat(Cno separator ';') |
+-----------+---------------------------------+
| 201215121 | 1;2;3                           |
| 201215122 | 2;3                             |
+-----------+---------------------------------+
2 rows in set (0.00 sec)

--将Sno去重横排显示
mysql> select Sno,group_concat(distinct Sno) from sc group by Sno;
+-----------+----------------------------+
| Sno       | group_concat(distinct Sno) |
+-----------+----------------------------+
| 201215121 | 201215121                  |
| 201215122 | 201215122                  |
+-----------+----------------------------+
2 rows in set (0.00 sec)

官方文档关于group_concat介绍,可以使用distinct,desc,order by,asc,separator分割符

 

 

 关于group_concat_max_len部分介绍,group_concat_max_len的默认值是1024Bit,可以设置的更大,但是最大值也受max_allowed_packet的影响

 

 

 

语法:

SET [GLOBAL | SESSION] group_concat_max_len = val;

 

mysql8.0可以直接持久化参数

mysql> show variables like '%group_concat%';
+----------------------+-------+
| Variable_name        | Value |
+----------------------+-------+
| group_concat_max_len | 1024  |
+----------------------+-------+
1 row in set (0.00 sec)

mysql> 
mysql> select @@version;
+-----------+
| @@version |
+-----------+
| 8.0.29    |
+-----------+
1 row in set (0.00 sec)

mysql> set persist group_concat_max_len=204800;
Query OK, 0 rows affected (0.00 sec)

 

 

posted @ 2022-08-22 10:29  我爱睡莲  阅读(352)  评论(0编辑  收藏  举报