<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>超群.com的博客 &#187; group_concat_max_len</title>
	<atom:link href="http://www.fuchaoqun.com/tag/group_concat_max_len/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fuchaoqun.com</link>
	<description></description>
	<lastBuildDate>Thu, 08 Sep 2011 15:08:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>MySQL统计函数GROUP_CONCAT陷阱</title>
		<link>http://www.fuchaoqun.com/2008/12/mysql-trap-of-group-concat/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mysql-trap-of-group-concat</link>
		<comments>http://www.fuchaoqun.com/2008/12/mysql-trap-of-group-concat/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 06:42:20 +0000</pubDate>
		<dc:creator>超群.com</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[group_concat]]></category>
		<category><![CDATA[group_concat_max_len]]></category>
		<category><![CDATA[max_join_size]]></category>
		<guid isPermaLink="false">http://chaoqun.17348.com/?p=105</guid>
		<description><![CDATA[本博客所有原创文章采用知识共享署名-非商业性使用-相同方式共享，转载请保留链接http://chaoqun.17348.com/2008/12/mysql-trap-of-group-concat/ 最近在用MySQL做一些数据的预处理，经常会用到group_concat函数，比如类似下面一条语句 mysql&#62;select aid,group_concat(bid) from tbl group by aid limit 1; sql语句比较简单，按照aid分组，并且把相应的bid用逗号串起来。这样的句子大家可能都用过，也可能不会出问题，但是如果bid非常多的话，你就要小心了，比如下面的提示信息： Query OK, XXX rows affected, 1 warning (3 min 45.12 sec) 怎么会有警告呢，打出来看看： mysql&#62; show warnings; +---------+------+-----------------------------------------+ &#124; Level   &#124; Code &#124; Message                                 &#124; +---------+------+-----------------------------------------+ &#124; Warning &#124; 1260 &#124; 1 line(s) were cut by GROUP_CONCAT() &#124; +---------+------+-----------------------------------------+ 居然被GROUP_CONCAT截断了我的结果，查了一下手册，原来GROUP_CONCAT有个最大长度的限制，超过最大长度就会被截断掉，你可以通过下面的语句获得： mysql&#62; SELECT @@global.group_concat_max_len; +-------------------------------+ &#124; @@global.group_concat_max_len [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>本博客所有原创文章采用<a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"><span style="color: #356aa0;">知识共享署名-非商业性使用-相同方式共享</span></a>，转载请保留链接<a href="http://chaoqun.17348.com/2008/12/mysql-trap-of-group-concat/" target="_blank">http://chaoqun.17348.com/2008/12/mysql-trap-of-group-concat/</a></p></blockquote>
<p>最近在用MySQL做一些数据的预处理，经常会用到group_concat函数，比如类似下面一条语句</p>
<blockquote><p>mysql&gt;select aid,group_concat(bid) from tbl group by aid limit 1;</p></blockquote>
<p>sql语句比较简单，按照aid分组，并且把相应的bid用逗号串起来。这样的句子大家可能都用过，也可能不会出问题，但是如果bid非常多的话，你就要小心了，比如下面的提示信息：</p>
<blockquote><p>Query OK, XXX rows affected, 1 warning (3 min 45.12 sec)</p></blockquote>
<p>怎么会有警告呢，打出来看看：</p>
<blockquote>
<pre>mysql&gt; show warnings;
+---------+------+-----------------------------------------+
| Level   | Code | Message                                 |
+---------+------+-----------------------------------------+
| Warning | 1260 | 1 line(s) were cut by GROUP_CONCAT()    |
+---------+------+-----------------------------------------+</pre>
</blockquote>
<p>居然被GROUP_CONCAT截断了我的结果，查了一下手册，原来GROUP_CONCAT有个最大长度的限制，超过最大长度就会被截断掉，你可以通过下面的语句获得：</p>
<blockquote>
<pre>mysql&gt; SELECT @@global.group_concat_max_len;
+-------------------------------+
| @@global.group_concat_max_len |
+-------------------------------+
|                      1024     |
+-------------------------------+</pre>
</blockquote>
<p>1024这就是一般MySQL系统默认的最大长度，如果你的bid串起来大于这个就会出问题，好在有解决的办法：</p>
<p>1.在MySQL配置文件中加上</p>
<blockquote><p>group_concat_max_len = 102400 #你要的最大长度</p></blockquote>
<p>2.可以简单一点，执行语句：</p>
<blockquote>
<pre>mysql&gt; SET GLOBAL group_concat_max_len=102400;
Query OK, 0 rows affected (0.01 sec)</pre>
</blockquote>
<p>再执行group_concat语句就不会出问题了，另外手册上还给出了group_concat的详细用法，给个示例你就明白了：</p>
<blockquote>
<pre>mysql&gt;select aid,group_concat(bid order by bid separator ',') as bid_str from tbl group by aid;</pre>
</blockquote>
<p>还可以排序和设置分隔符，功能强大。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fuchaoqun.com/2008/12/mysql-trap-of-group-concat/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
