MySQL Truncating of result when using Group_Concat and Concat

后端 未结 2 1541
后悔当初
后悔当初 2020-12-18 03:13

I have the following SQL (I have removed some of the selesct fi:

SELECT node_revisions.title                          AS \'Task\', 
       node_revisions.bod         


        
相关标签:
2条回答
  • 2020-12-18 03:45

    As Andre mentioned, the result of GROUP_CONCAT() is limited to group_concat_max_len bytes.

    However, when using GROUP_CONCAT with ORDER BY, the result is further truncated to one third of group_concat_max_len. This is why your original result was being truncated to 341 (= 1024/3) bytes. If you remove the ORDER BY clause, this should return up to the full 1024 bytes for Comments. For example:

    CREATE TABLE MyTable
    (
        `Id` INTEGER,
        `Type` VARCHAR(10),
        `Data` TEXT
    );
    
    INSERT INTO MyTable VALUES
    (0, 'Alpha', 'ABCDEF'),
    (1, 'Alpha', 'GHIJKL'),
    (2, 'Alpha', 'MNOPQR'),
    (3, 'Alpha', 'STUVWX'),
    (4, 'Alpha', 'YZ'),
    (5, 'Numeric', '12345'),
    (6, 'Numeric', '67890');
    
    SET SESSION group_concat_max_len = 26;
    
    -- Returns 26 bytes of data
    SELECT Type, GROUP_CONCAT(Data SEPARATOR '') AS AllData_Unordered
    FROM MyTable
    GROUP BY Type;
    
    -- Returns 26/3 = 8 bytes of data
    SELECT Type, GROUP_CONCAT(Data SEPARATOR '') AS AllData_Ordered
    FROM MyTable
    GROUP BY Type
    ORDER BY Id;
    
    DROP TABLE MyTable;
    

    Will return

    Type    AllData_Unordered
    Alpha   ABCDEFGHIJKLMNOPQRSTUVWXYZ
    Numeric 1234567890
    
    Type    AllData_Ordered
    Alpha   ABCDEFGH
    Numeric 12345678
    

    I have not found this interaction between GROUP_CONCAT() and ORDER BY mentioned in the MySQL Manual, but it affects at least MySQL Server 5.1.

    0 讨论(0)
  • 2020-12-18 03:59

    GROUP_CONCAT() is, by default, limited to 1024 bytes.

    To work around this limitation and allow up to 100 KBytes of data, add group_concat_max_len=102400 in my.cnf or query the server using SET GLOBAL group_concat_max_len=102400.

    0 讨论(0)
提交回复
热议问题