解决方法
一种选择是用空格#
填充您的值。所以 group_concat 中的每个项目都是相同的长度。
让我们假设没有超过 20 个字符的项目。
那么您的查询将是:
SET group_concat_max_len = 10*20+9; /*execute this first.*/
/*10 items, 20 bytes each + 9 bytes for the separator*/
SELECT country,
REPLACE(GROUP_CONCAT(RIGHT(CONCAT(repeat('#',20),docID),20)),'#','') AS docIDs,
REPLACE(GROUP_CONCAT(RIGHT(CONCAT(repeat('#',20),analyst),20)),'#','') AS analysts,
REPLACE(GROUP_CONCAT(RIGHT(CONCAT(repeat('#',20),region,20)),'#','') AS regions,
REPLACE(GROUP_CONCAT(RIGHT(CONCAT(repeat('#',20),report,20)),'#','') AS reports,
REPLACE(GROUP_CONCAT(RIGHT(CONCAT(repeat('#',20),topic,20)),'#','') AS topics,
MAX((date)) AS `date`, /* LATEST DATE*/
MAX((docID)) AS docID, /* LATEST DOC*/
REPLACE(GROUP_CONCAT(RIGHT(CONCAT(repeat('#',20),date,20)),'#','') AS dates,
REPLACE(GROUP_CONCAT(RIGHT(CONCAT(repeat('#',20),event,20)),'#','') AS events,
REPLACE(GROUP_CONCAT(RIGHT(CONCAT(repeat('#',20),province,20)),'#','') AS provinces
FROM reports
GROUP BY country ORDER BY `date` DESC, docID DESC
回顾一下:
x= CONCAT(repeat('#',20),docID) adds 20 x #################### in front of docID
y= RIGHT(X,20) Takes the rightmost 20 chars of that
z= GROUP_CONCAT(y) strings these together up to max_len
result = REPLACE(z,'#','') removes the `#` so the result looks normal.
或者编写自己的 group_concat 版本
可以使用自己的 group_concat UDF。
网上流传着几个例子。
例如:http ://www.codeproject.com/KB/database/mygroupconcat.aspx?display=Mobile