当前位置: 移动技术网 > IT编程>数据库>Mysql > Mysql GROUP_CONCAT使用注意事项说明

Mysql GROUP_CONCAT使用注意事项说明

2018年11月05日  | 移动技术网IT编程  | 我要评论
group_concat 函数返回一个字符串结果,该结果由分组中的值连接组合而成,常和 group by 连用。 如果需要自定义分隔符可以使用 separator。 示例: select group

group_concat 函数返回一个字符串结果,该结果由分组中的值连接组合而成,常和 group by 连用。

如果需要自定义分隔符可以使用 separator。

示例:

select group_concat(id) ids from sys_oem_resources where pid is not null group by pid;

\

select group_concat(id separator '*') ids from sys_oem_resources where pid is not null group by pid;

\

注意示例:

别乱用group_concat,说不定你现在使用的group_concat 得到的结果就是隐藏着bug的。

group_concat 的结果会受到group_concat_max_len 变量的限制。

默认group_concat_max_len = 1024,即字符串的长度字节超过1024 则会被截断。

通过命令 "show variables like 'group_concat_max_len' " 来查看 group_concat默认的长度:

mysql> show variables like 'group_concat_max_len';

+----------------------+-------+

| variable_name | value |

+----------------------+-------+

| group_concat_max_len | 1024 |

+----------------------+-------+

1 row in set

在mysql配置文件中添加配置:group_concat_max_len = -1 (-1为最大值或根据实际需求设置长度),配置后需要重启mysql服务,查看如下所示:

mysql> show variables like 'group_concat_max_len';

+----------------------+------------+

| variable_name | value |

+----------------------+------------+

| group_concat_max_len | 4294967295 |

+----------------------+------------+

1 row in set

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网