mysql - 按文本字段分组时的 GROUP_CONCAT() 行数

标签 mysql select group-by group-concat

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_CONCAT() 的情况下运行 GROUP BY 查询时,结果集符合预期(显示两行,每行对应 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(另外,您可能会注意到,我在 id 字段上使用 GROUP_CONCAT(),而不是 text 字段,其结果甚至没有超过 GROUP_CONCAT_MAX_LEN)。

最佳答案

您必须根据需要将 ma​​x_sort_length 明智地或全局地更改为更高数量的 session 。默认情况下,它的值为 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 DEMO

编辑:BLOBTEXT 值不能可靠地用于GROUP BY ORDER BYDISTINCT。在这些情况下比较 BLOB 值时,仅使用前 ma​​x_sort_length 字节。 ma​​x_sort_length 的默认值为 1024,可以在服务器启动时或运行时更改。

关于mysql - 按文本字段分组时的 GROUP_CONCAT() 行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12862968/

相关文章:

MySQL 如何在任何 15 分钟的窗口中总结过多的交易量?

php - 查询搜索 Laravel 5

PHP 5.5.27 - 来自一列公共(public)数据的数组内的行数组 - 如何?

MySQL XML 获取第一个 child

mysql - sql多元素选择

mysql - 我的sql Group By案例获得特定年份的最高薪员工

php - 使用准备语句时如何创建多维数组?

jquery - 使用 jQuery 和 JSON 自动填充选择标签

sql - 两个 T-SQL 查询的交集

javascript - 根据分组属性加入 2 个对象数组