开发者

MySQL Truncating of result when using Group_Concat and Concat

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

SELECT node_revisions.title                          AS 'Task', 
       node_revisions.body                           AS 'Description', 
       Date_format(field_due_date_value, '%e/%c/%Y') AS 'Due Date', 
       开发者_StackOverflowusers.name                                    AS 'User Name', 
       (SELECT GROUP_CONCAT(Concat(CHAR(10),Concat_ws( ' - ', name, From_unixtime( TIMESTAMP, 
               '%e/%c/%Y' )),CHAR(10),COMMENT))
        FROM   comments 
        WHERE  comments.nid = content_type_task.nid) AS 'Comments' 
FROM   content_type_task 
       INNER JOIN users 
         ON content_type_task.field_assigned_to_uid = users.uid 
       INNER JOIN node_revisions 
         ON content_type_task.vid = node_revisions.vid 
ORDER  BY content_type_task.nid DESC 

This pulls back all my tasks and all comments associated with a task. The problem I am having is that the comments field; created using the *GROUP_CONCAT*, is truncating the output. I don't know why and I don't know how to overcome this. (It looks to be at 341ish chars)


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜