MySQL 和 GROUP_CONCAT() 最大长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2567000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:42:05  来源:igfitidea点击:

MySQL and GROUP_CONCAT() maximum length

mysqlgroup-concat

提问by ZeWaren

I'm using GROUP_CONCAT()in a MySQL query to convert multiple rows into a single string. However, the maximum length of the result of this function is 1024characters.

GROUP_CONCAT()在 MySQL 查询中使用将多行转换为单个字符串。但是,此函数的结果的最大长度是1024字符。

I'm very well aware that I can change the param group_concat_max_lento increase this limit:

我非常清楚我可以更改参数group_concat_max_len以增加此限制:

SET SESSION group_concat_max_len = 1000000;

However, on the server I'm using, I can't change any param. Not by using the preceding query and not by editing any configuration file.

但是,在我使用的服务器上,我无法更改任何参数。不是通过使用前面的查询,也不是通过编辑任何配置文件。

So my question is: Is there any other way to get the output of a multiple row query into a single string?

所以我的问题是:有没有其他方法可以将多行查询的输出转换为单个字符串?

采纳答案by ZeWaren

CREATE TABLE some_table (
  field1 int(11) NOT NULL AUTO_INCREMENT,
  field2 varchar(10) NOT NULL,
  field3 varchar(10) NOT NULL,
  PRIMARY KEY (`field1`)
);

INSERT INTO `some_table` (field1, field2, field3) VALUES
(1, 'text one', 'foo'),
(2, 'text two', 'bar'),
(3, 'text three', 'data'),
(4, 'text four', 'magic');

This query is a bit strange but it does not need another query to initialize the variable; and it can be embedded in a more complex query. It returns all the 'field2's separated by a semicolon.

这个查询有点奇怪,但它不需要另一个查询来初始化变量;它可以嵌入到更复杂的查询中。它返回所有由分号分隔的'field2'。

SELECT result
FROM   (SELECT @result := '',
               (SELECT result
                FROM   (SELECT @result := CONCAT_WS(';', @result, field2) AS result,
                               LENGTH(@result)                            AS blength
                        FROM   some_table
                        ORDER  BY blength DESC
                        LIMIT  1) AS sub1) AS result) AS sub2; 

回答by keatkeat

SET SESSION group_concat_max_len = 1000000;

is a temporary, session-scope, setting. It only applies to the current session You should use it like this.

是一个临时的、会话范围的设置。它仅适用于当前会话您应该像这样使用它。

SET SESSION group_concat_max_len = 1000000;
select group_concat(column) from table group by column

You can do this even in sharing hosting, but when you use an other session, you need to repeat the SET SESSIONcommand.

您甚至可以在共享主机中执行此操作,但是当您使用其他会话时,您需要重复该SET SESSION命令。

回答by oscar

The correct parameter to set the maximum length is:

设置最大长度的正确参数是:

SET @@group_concat_max_len = value_numeric;

value_numericmust be > 1024; by default the group_concat_max_lenvalue is 1024.

value_numeric必须 > 1024;默认group_concat_max_len值为 1024。

回答by Chinnadurai Ramalingam

Include this setting in xampp my.ini configuration file:

在 xampp my.ini 配置文件中包含此设置:

[mysqld]
group_concat_max_len = 1000000

Then restart xampp mysql

然后重启xampp mysql

回答by Mohamed El Mrabet

You can try this

你可以试试这个

SET GLOBAL group_concat_max_len = 1000000;

回答by Ola Balstad

The correct syntax is mysql> SET @@global.group_concat_max_len = integer;
If you do not have the privileges to do this on the server where your database resides then use a query like:
mySQL="SET @@session.group_concat_max_len = 10000;"or a different value.
Next line:
SET objRS = objConn.Execute(mySQL) your variables may be different.
then
mySQL="SELECT GROUP_CONCAT(......);"etc
I use the last version since I do not have the privileges to change the default value of 1024 globally (using cPanel).
Hope this helps.

正确的语法是 mysql>SET @@global.group_concat_max_len = integer;
如果您没有在数据库所在的服务器上执行此操作的权限,则使用如下查询:
mySQL="SET @@session.group_concat_max_len = 10000;"或其他值。
下一行:
SET objRS = objConn.Execute(mySQL) 您的变量可能不同。
然后
mySQL="SELECT GROUP_CONCAT(......);"
我使用最后一个版本,因为我没有权限全局更改 1024 的默​​认值(使用 cPanel)。
希望这可以帮助。