GROUP_CONCAT () counts rows when grouping by text field

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'); 

When you run a GROUP BY query without using GROUP_CONCAT() result set looks as expected (showing two lines, one for each text option):

 SELECT `text` FROM `table` GROUP BY `text`; +-----------------------------------+ | text | +-----------------------------------+ | Unpacked reserved sir offering... | | Unpacked reserved sir offering... | +-----------------------------------+ 2 rows in set (0.02 sec) 

However, when starting the same query with GROUP_CONCAT() result set is not expected (showing one row with a concatenated string of two id fields):

 SELECT GROUP_CONCAT(`id` SEPARATOR ', ') AS ids FROM `table` GROUP BY `text`; +------+ | ids | +------+ | 1, 2 | +------+ 1 row in set (0.00 sec) 

My question is:

Why does using GROUP_CONCAT() affect the number of rows returned?

My initial assumption was that GROUP_CONCAT_MAX_LEN has something to do with it (my value is 1024), but it certainly only affects GROUP_CONCAT() , not GROUP BY (as you can see, I use GROUP_CONCAT() in the id field, and not in the text field, and the result of this is not even close to exceeding GROUP_CONCAT_MAX_LEN ).

+8
mysql select group-by group-concat
source share
2 answers

You should change max_sort_length to a higher session session or globally according to your needs. By default, its value is 1024 , and your line contains 1170 data. By increasing the size, it will give two rows for GROUP_CONCAT .

Check this link 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`; 

Check SQL FIDDLE DEMO

EDIT: BLOB and TEXT cannot be reliably used in GROUP BY , ORDER BY, or DISTINCT . When comparing BLOB values ​​in these cases, only the first bytes of max_sort_length are used . The default value of max_sort_length is 1024 and can be changed at server startup or at runtime.

+5
source share

It seems you are using MySQL by default GROUP_CONCAT_MAX_LEN. Your string has a length of 1178, which definitely exceeds the default value of 1024. This means that if the values ​​differ somewhat later than 1024, MySQL simply ignores it, because the first 1024 characters are exactly identical. This is a restriction on the behavior of GROUP_CONCAT, not GROUP.

You can do this more in the my.cnf file for MySQL.

See here for more details:

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

+1
source share

All Articles