9
DROP TABLE IF EXISTS `table`;
CREATE TABLE `table` (
  `id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
  `text` text COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `table` VALUES ('1', 'Unpacked reserved sir offering bed judgment may and quitting speaking. Is do be improved raptures offering required in replying raillery. Stairs ladies friend by in mutual an no. Mr hence chief he cause. Whole no doors on hoped. Mile tell if help they ye full name. \r\n\r\nLadyship it daughter securing procured or am moreover mr. Put sir she exercise vicinity cheerful wondered. Continual say suspicion provision you neglected sir curiosity unwilling. Simplicity end themselves increasing led day sympathize yet. General windows effects not are drawing man garrets. Common indeed garden you his ladies out yet. Preference imprudence contrasted to remarkably in on. Taken now you him trees tears any. Her object giving end sister except oppose. \r\n\r\nWas justice improve age article between. No projection as up preference reasonably delightful celebrated. Preserved and abilities assurance tolerably breakfast use saw. And painted letters forming far village elderly compact. Her rest west each spot his and you knew. Estate gay wooded depart six far her. Of we be have it lose gate bred. Do separate removing or expenses in. Had covered but evident chapter matters anxious.');
INSERT INTO `table` VALUES ('2', 'Unpacked reserved sir offering bed judgment may and quitting speaking. Is do be improved raptures offering required in replying raillery. Stairs ladies friend by in mutual an no. Mr hence chief he cause. Whole no doors on hoped. Mile tell if help they ye full name. \r\n\r\nLadyship it daughter securing procured or am moreover mr. Put sir she exercise vicinity cheerful wondered. Continual say suspicion provision you neglected sir curiosity unwilling. Simplicity end themselves increasing led day sympathize yet. General windows effects not are drawing man garrets. Common indeed garden you his ladies out yet. Preference imprudence contrasted to remarkably in on. Taken now you him trees tears any. Her object giving end sister except oppose. \r\n\r\nWas justice improve age article between. No projection as up preference reasonably delightful celebrated. Preserved and abilities assurance tolerably breakfast use saw. And painted letters forming far village elderly compact. Her rest west each spot his and you knew. Estate gay wooded depart six far her. Of we be have it lose gate bred. Do separate removing or expenses in. Had covered but evident chapter matters anxious');

在不使用结果集的情况下运行GROUP BY查询时GROUP_CONCAT()符合预期(显示两行,每个变体一个text):

SELECT
    `text`
FROM
    `table`
GROUP BY
    `text`;

+-----------------------------------+
| text                              |
+-----------------------------------+
| Unpacked reserved sir offering... |
| Unpacked reserved sir offering... |
+-----------------------------------+
2 rows in set (0.02 sec)

但是,当使用结果集运行相同的查询时GROUP_CONCAT(),结果集不符合预期(显示一行具有两个id字段的串联字符串):

SELECT
    GROUP_CONCAT(`id` SEPARATOR ', ') AS ids
FROM
    `table`
GROUP BY
    `text`;

+------+
| ids  |
+------+
| 1, 2 |
+------+
1 row in set (0.00 sec)

我的问题:

为什么使用GROUP_CONCAT()会影响返回的行数?

我最初的假设是GROUP_CONCAT_MAX_LEN与它有关(我的设置为 1024)但肯定只会影响GROUP_CONCAT(),而不是GROUP BY(而且,正如您可能注意到的,我GROUP_CONCAT()id场上使用,而不是text场上,以及结果甚至没有接近超过GROUP_CONCAT_MAX_LEN)。

4

2 回答 2

5

您必须根据需要将max_sort_length更改为更高数量的会话或全局。默认情况下,它的值为1024字节,您的字符串包含1170字节的数据。通过增加大小,它将为GROUP_CONCAT提供两行。

检查此链接max_sort_length

SELECT `text` FROM `table` GROUP BY `text`;

SET SESSION max_sort_length = 2000;
SELECT GROUP_CONCAT(`id` SEPARATOR ', ') AS ids FROM `table` GROUP BY `text`;

检查SQL FIDDLE 演示

编辑: BLOBTEXT值不能可靠地用于GROUP BYORDER BYDISTINCT。在这些情况下比较 BLOB 值时,只使用第一个max_sort_length字节。max_sort_length的默认值为1024,可以在服务器启动时或运行时更改。

于 2013-01-04T11:27:57.817 回答
1

看来您遇到了 MySQL 的默认 GROUP_CONCAT_MAX_LEN。您的字符串长度为 1178,这肯定超过了默认值 1024。这意味着,如果值的差异晚于 1024,MySQL 将简单地忽略它,因为前 1024 个字符完全相同。这是对 GROUP_CONCAT 行为的限制,而不是对 GROUP 的限制。

您可以在 MySQL 的 my.cnf 文件中将其放大。

有关更多详细信息,请参见此处:

http://www.coderanch.com/t/422632/JDBC/databases/increase-group-concat-max-len

于 2012-10-12T16:27:35.350 回答