MySQL GROUP_CONCAT 砍掉一半的数据(有时)
Posted
技术标签:
【中文标题】MySQL GROUP_CONCAT 砍掉一半的数据(有时)【英文标题】:MySQL GROUP_CONCAT chopping off half the data (sometimes) 【发布时间】:2021-12-21 05:50:00 【问题描述】:我似乎有一个非常奇怪的问题...以下查询截断了GROUP_CONCAT
的一些结果
select
booking_id,
group_concat(booking_resource_info_grouped_bla)
from (
SELECT
b.id AS booking_id,
GROUP_CONCAT(DISTINCT booking_resource_info SEPARATOR 'hr') AS `booking_resource_info_grouped_bla`
FROM
(
SELECT 492185 AS id, '25ft Climbing 1' as booking_resource_info
UNION ALL
SELECT 492185 AS id, '25ft Climbing 2' as booking_resource_info
UNION ALL
SELECT 492185 AS id, '3 G Swing' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Archery 1' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Body Boarding' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Canadian Canoes' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Catering 1' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Crate Stack 1' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Kayak 1' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Kayak 2' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Orange Lodge' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Lake' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Low Ropes' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Marquee 1 - Site 3' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Parker Lodge' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Raft Building 1' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Raft Building 2' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Raft Building 3' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'School Package 1' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Apple Lodge' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Team Building 1' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Tomahawks' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Banana Lodge' as booking_resource_info
UNION ALL
SELECT 492185 AS id, 'Wobble Pole' as booking_resource_info
) b
GROUP BY b.id
UNION ALL
SELECT
123 AS booking_id,
'' AS `booking_resource_info_grouped_bla`
) test
GROUP BY booking_id
;
这会产生:
25ft Climbing 1hr25ft Climbing 2hr3 G SwinghrApple LodgehrArchery 1hrBanana Lodgehr身体 登机hr加拿大独木舟hr餐饮 1hr板条箱堆栈 1hr皮划艇 1hr皮划艇 2hrKiwi Lodgehr湖hr低 绳索hrMarquee 1 - Site 3hrOrange Lodgehr木筏建筑 1hrRaft Building 2hrRaft Bui
如你所见,尾部Raft Bui
不完整。
另一方面,我已经让这个查询正常工作。我让它在另一台服务器上工作,所以我重新启动了其中一个,然后那个也开始工作了。
我在 mysql 5.6 和 5.7 上都进行了广泛的尝试——这两个版本都产生了预期和未预期的结果。 我在 db-fiddle.com 上尝试了 MySQL 8.0,这是唯一产生预期结果的版本。
是否有我缺少的设置/内存限制/已知问题?
值得注意的是,当查询的 UNION ALL
部分被删除时,此查询始终有效,因此其中存在一个虚拟联合,因此问题是可重新创建的。
【问题讨论】:
select @@session.group_concat_max_len
在您遇到“问题”的服务器上返回什么?
【参考方案1】:
SET SESSION group_concat_max_len = 1000000;
这将解决您的问题。 默认组 concat 的最大长度为 1024
更多信息:https://www.namasteui.com/mysql-group_concat-maximum-length/
【讨论】:
是的,这似乎有效,谢谢! 看起来很奇怪,它总是在没有最终联合的情况下工作。也许这会加倍长度或类似的奇怪东西。UNION
without ALL
将删除重复项,因此 (foo),(foo),(bar)
(3 行)将变为 (foo),(bar)
(2 行)以上是关于MySQL GROUP_CONCAT 砍掉一半的数据(有时)的主要内容,如果未能解决你的问题,请参考以下文章