mysql - 如何复制一行 50 次并在 MySQL 中使用自动增量 id 字段插入到同一个表中?
问题描述
在 MySQL 中,我试图复制具有自动增量列 ID=k 的行,并将其插入到同一个表中作为新行。假设表中有 10 行,因此执行查询后表中将有 500 行。
如何在单个查询中执行此操作?
解决方案
您可以使用递归 CTE 生成数字,然后基于该值插入:
insert into t ( . . . ) -- all columns besides the auto increment column
with recursive cte as (
select 1 as n
union all
select n + 1
from cte
where n < 49
)
select . . . -- all columns but the auto increment column
from t cross join
cte;
如果您想以 500 行结束,“49”可以是动态的。以上将现有行再插入 49 次。
推荐阅读
- excel - 使用 UDF 块 Worksheet_change 执行条件格式化
- c# - 如何在 webapi 中将数据写入 Excel
- sql-server - 无法连接到在打开几个连接后死亡的 Docker 的 SQL Server 实例
- javascript - 多个对话框。如何检测哪一个是焦点?
- c# - 遇到 MySQL 异常问题
- javascript - 将 JavaScript 添加到 php 脚本
- javascript - Angular 5手动设置反应形式的错误没有反映在dom(IE / IOS)
- typescript - 在 vue typescript 项目中使用 vuejs 组件
- wpf - 在 MaterialDesignInXamlToolkit 中使用来自不同样本的颜色
- sas - 如何编写 SAS 代码来打开 Viewtable 窗口?