【发布时间】:2020-08-04 08:08:27
【问题描述】:
【问题讨论】:
-
你为什么要这么做?
-
你的 MySql/MariaDB 版本是多少?
【问题讨论】:
如果您的 MySql/MariaDB 版本支持窗口函数并且您已经创建了 TableTwo,您可以像这样插入新行:
insert into TableTwo(id, num)
select
id,
concat(
coalesce(lag(right(num, 3)) over (order by id), 'xxx'),
coalesce(left(num, 2), '')
) num
from (
select * from TableOne
union all
select max(id) + 1, null from TableOne
) t;
请参阅demo。
如果没有窗口函数,您可以使用自连接来完成:
insert into TableTwo(id, num)
select
t.id,
concat(
coalesce(right(t1.num, 3), 'xxx'),
coalesce(left(t.num, 2), '')
) num
from (
select * from TableOne
union all
select max(id) + 1, null from TableOne
) t left join TableOne t1
on t1.id = t.id - 1;
请参阅demo。
结果:
> id | num
> -: | :----
> 1 | xxxab
> 2 | cde01
> 3 | 23456
> 4 | 789
【讨论】: