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()と、期待どおりになります ( の各バリエーションに 1 つずつ、2 つの行が表示されます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()期待どおりになりません (2 つのフィールドの連結された文字列を含む 1 つの行が表示され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()usingが返される行数に影響するのはなぜですか?

私の最初の仮定はGROUP_CONCAT_MAX_LEN、それと関係があるということでした (私の場合は 1024 に設定されています) が、それは にのみ影響しGROUP_CONCAT()、影響しませんGROUP BY(また、お気づきかもしれませんが、私はフィールドではなくフィールドで使用GROUP_CONCAT()しており、その結果を超えることさえありません)。idtextGROUP_CONCAT_MAX_LEN

4

2 に答える 2

5

必要に応じて、 max_sort_lengthをセッションごとまたはグローバルに大きい数値に変更する必要があります。デフォルトでは、その値は1024バイトであり、文字列には1170バイトのデータが含まれています。サイズを大きくすると、GROUP_CONCATに2行が表示されます。

このリンクを確認してください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`;

SQLFIDDLEDEMOを確認してください

編集: BLOBおよびTEXT値は、 GROUP BYORDER BY、またはDISTINCTで確実に使用することはできません。このような場合にBLOB値を比較するときは、最初のmax_sort_lengthバイトのみが使用されます。max_sort_lengthのデフォルト値は1024であり、サーバーの起動時または実行時に変更できます。

于 2013-01-04T11:27:57.817 に答える
1

MySQL のデフォルトの GROUP_CONCAT_MAX_LEN を実行しているようです。文字列の長さは 1178 で、デフォルト値の 1024 を確実に超えています。つまり、値が 1024 より後の値で異なる場合、最初の 1024 文字がまったく同じであるため、MySQL は単純にそれを無視します。これは、GROUP ではなく、GROUP_CONCAT の動作に対する制限です。

MySQL の my.cnf ファイルでこれを大きくすることができます。

詳細については、こちらを参照してください。

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

于 2012-10-12T16:27:35.350 に答える