我在 MySQL 8 上有这个表:
create table tbl(id varchar(2), val int);
insert into tbl values ('A', 1), ('B', 2), ('C', 3), ('D', 4), ('E', 5);
Run Code Online (Sandbox Code Playgroud)
以下查询应找出哪些记录集的值加起来不大于 6(不考虑顺序):
with recursive cte(greatest_id, ids, total) as (
select id,
concat(id, ''),
val
from tbl
union all
select tbl.id,
concat(cte.ids, tbl.id),
cte.total + tbl.val
from cte
inner join tbl
on tbl.id > cte.greatest_id
and cte.total + tbl.val <= 6
)
select ids, total from cte
Run Code Online (Sandbox Code Playgroud)
运行它会导致以下错误:
错误:ER_DATA_TOO_LONG:
concat(id, '')第 7行列的数据太长
为什么 MySQL 会产生这个错误? …