出处:http://blog.csdn.net/wujiangwei567/article/details/42418547
以下为全文转载:
【group_concat】
该函数的作用就是将,查询到的结果以 ',' 分隔的
方式串联起来组成一个字符串,经常地,我们可以将它的结果拼接在
sql的in子句中,来做可选参数,非常方便
- 如:select group_concat(id) ids from userinfo where usertype='vip'
但是数据量一但大了就会出意想不到的问题。
这是为什么呢;因为MySql默认给 该函数结果的长度是1024
我们可以用命令查出来:
- mysql> show variables like '%group_concat%';
- +
- | Variable_name | Value |
- +
- | group_concat_max_len | 1024 |
- +
- 1 row in set (0.00 sec)
可以改吗,答案是肯定的,这里我们从两个方面探讨这个问题;
1、从mysql着手
①临时性的更改
我们可以在查询之前添加子句
- mysql> set group_concat_max_len = 99999;
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> show variables like '%group_con%';
- +
- | Variable_name | Value |
- +
- | group_concat_max_len | 99999 |
- +
可不可以无限大,可以将值 改为 -1 。
但是这会有一个问题,断开连接后,值又恢复到 1024,那么我们看第二种方法
②永久性修改
我们找到my.ini 或 my.conf 文件
该变量是没有在这些文件中声明的,我们只需要加上下面这句即可:
- group_concat_max_len = -1 # -1为最大值或填入你要的最大长度
然后重启MySql ,ok没问题了
2、我们从PHP的角度着手
从程序的角度看,我们也不用大动干戈,做什么呢,做一个循环,我们照样能连接
- $sql = "select id from from userinfo where usertype='vip'";
接着我们将结果循环出来;
- $arr = array();
- foreach($res as $id){
- $arr[] = $id;
- }
- $ids = implode(',',$arr);
ok,现在也大功告成了!!!