繁体   English   中英

如何使用增加的主键将表的数据复制到另一个重复的表以及使用增加的值的某些列。 在MySQL中

[英]How to copy table's data to another duplicate table with incremented primary key and some of the column with incremented value. in mySQL

我有一个表“ Channels”,其中包含一个主ID列(即1,2,3,..),一个Incremented SPL_ID列(即1000,1001,1002,...)和另外30个columnn包含不同的记录。

我需要的是:将所有Channels数据插入哑元中,然后将哑元的记录复制回Channels中,这样我们就有10000条记录,那么我们就可以用该记录制作20000、30000等。

我创建了另一个虚拟表“虚拟”,将所有Channels数据插入到虚拟表中。

 Create table dummy like Channels;
 Insert into dummy select * from Channels;

现在,我需要将所有虚拟数据重新插入Channels表。

  insert into Channels select * from dummy.

问题 :主键以及如何管理splColumn,以便它可以在最后一个字段的值之后具有记录值,即最后一个字段1110,那么它应该为1111、1112、1113 ...

仅用于expl:

表格:频道

id-spl-其他列

1-10-abcd

2-11-abcde

现在将虚拟记录重新插入到通道,此时需要SPL值12、13 ..etc

将批量记录从一个表复制到另一个表时,您不能在任何非auto_incremented列上生成序列号。 特别是在使用通配符 select一起生成表和数据时。 对于使用选定字段创建的任何表,您都不能指定一列来从特定值开始按顺序分配值。

为此,您需要一个存储过程或首先使用所需字段创建表,然后将其插入其中。

范例

第一步

create table prime_copy( col1 ..., col2 ...,   
                         spl int not null auto_increment primary key,  
                         colX ..., colY ... );  

第二步

select max( splColumn ) + 1 into @ai from prime_table;  
set @sql := concat( 'alter table prime_copy auto_increment=', @ai );  
prepare stmt from @sql;
execute stmt;
drop prepare stmt;

第三步

-- reset the table, but not auto increment value
delete from prime_copy; 

insert into prime_copy( col1, col2, NO_Spl_Col_here, but_other_required )
  select colM, colN, NO_Spl_Col, but_other_required from prime_table;

insert into prime_table( col, names, splCol, as_required )
  select col, names, auto_incremented_spl_col, as_required );

重复Step3直到将所需数量的记录插入prime_table

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM