MySQL 和 GROUP_CONCAT() 最大长度

发布于 2024-08-28 00:55:24 字数 335 浏览 10 评论 0原文

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

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

SET SESSION group_concat_max_len = 1000000;

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

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

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 1024 characters.

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

无人接听 2024-09-04 00:55:24
SET SESSION group_concat_max_len = 1000000;

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

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

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

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 SESSION command.

一腔孤↑勇 2024-09-04 00:55:24

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

SET @@group_concat_max_len = value_numeric;

value_numeric 必须 > 1024;默认情况下,group_concat_max_len 值为 1024。

The correct parameter to set the maximum length is:

SET @@group_concat_max_len = value_numeric;

value_numeric must be > 1024; by default the group_concat_max_len value is 1024.

猫弦 2024-09-04 00:55:24

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

[mysqld]
group_concat_max_len = 1000000

然后重新启动 xampp mysql

Include this setting in xampp my.ini configuration file:

[mysqld]
group_concat_max_len = 1000000

Then restart xampp mysql

記柔刀 2024-09-04 00:55:24

你可以试试这个

SET GLOBAL group_concat_max_len = 1000000;

You can try this

SET GLOBAL group_concat_max_len = 1000000;
落在眉间の轻吻 2024-09-04 00:55:24

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

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.

万劫不复 2024-09-04 00:55:24

简短的回答:需要在建立与 MySQL 服务器的连接时设置该设置。例如,如果使用 MYSQLi / PHP,它将看起来像这样:

$ myConn = mysqli_init(); 
$ myConn->options(MYSQLI_INIT_COMMAND, 'SET SESSION group_concat_max_len = 1000000');

因此,如果您使用自制框架,那么,您需要在建立连接时在代码中查找位置并提供一个合理的值。

2020年我还在使用Codeigniter 3,所以在这个框架中,要添加的代码在application/system/database/drivers/mysqli/mysqli_driver.php中,函数名为db_connect();

public function db_connect($persistent = FALSE)
    {
        // Do we have a socket path?
        if ($this->hostname[0] === '/')
        {
            $hostname = NULL;
            $port = NULL;
            $socket = $this->hostname;
        }
        else
        {
            $hostname = ($persistent === TRUE)
                ? 'p:'.$this->hostname : $this->hostname;
            $port = empty($this->port) ? NULL : $this->port;
            $socket = NULL;
        }

        $client_flags = ($this->compress === TRUE) ? MYSQLI_CLIENT_COMPRESS : 0;
        $this->_mysqli = mysqli_init();

        $this->_mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 10);
        $this->_mysqli->options(MYSQLI_INIT_COMMAND, 'SET SESSION group_concat_max_len = 1000000');

...
    }

The short answer: the setting needs to be setup when the connection to the MySQL server is established. For example, if using MYSQLi / PHP, it will look something like this:

$ myConn = mysqli_init(); 
$ myConn->options(MYSQLI_INIT_COMMAND, 'SET SESSION group_concat_max_len = 1000000');

Therefore, if you are using a home-brewed framework, well, you need to look for the place in the code when the connection is establish and provide a sensible value.

I am still using Codeigniter 3 on 2020, so in this framework, the code to add is in the application/system/database/drivers/mysqli/mysqli_driver.php, the function is named db_connect();

public function db_connect($persistent = FALSE)
    {
        // Do we have a socket path?
        if ($this->hostname[0] === '/')
        {
            $hostname = NULL;
            $port = NULL;
            $socket = $this->hostname;
        }
        else
        {
            $hostname = ($persistent === TRUE)
                ? 'p:'.$this->hostname : $this->hostname;
            $port = empty($this->port) ? NULL : $this->port;
            $socket = NULL;
        }

        $client_flags = ($this->compress === TRUE) ? MYSQLI_CLIENT_COMPRESS : 0;
        $this->_mysqli = mysqli_init();

        $this->_mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 10);
        $this->_mysqli->options(MYSQLI_INIT_COMMAND, 'SET SESSION group_concat_max_len = 1000000');

...
    }
云胡 2024-09-04 00:55:24
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');

这个查询有点奇怪,但它不需要另一个查询来初始化变量;并且它可以嵌入到更复杂的查询中。
它返回所有由分号分隔的“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; 
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.

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; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文