简体   繁体   English

将一个表中的行插入到另一个表中,哪一个具有额外的列?

[英]Insert row from one table to another table, which has extra column?

I am working on migration of data from one db1 to another db2. 我正在将数据从一个db1迁移到另一个db2。

I have one scenario, where table Person is present in both databases(db1 and db2). 我有一种情况,两个数据库(db1和db2)中都存在表Person。

But the table in db2 has one extra column. 但是db2中的表有一个额外的列。 Which is best effective way to write the query to migrate data from db1 Person to db2 Person. 这是编写查询以将数据从db1 Person迁移到db2 Person的最有效方法。

I have written as below, is this is the best way to write it? 我写的如下,这是最好的写法吗? Because if i have more columns and only one extra column, due to which i need to mention all the column names in loop statement. 因为如果我有更多列并且只有一个额外的列,由于这个原因,我需要在循环语句中提及所有列名。

$select = $dbh1->prepare("SELECT * FROM person");
$insert = $dbh2->prepare("INSERT INTO PERSON VALUES (?,?,?,?,?)");

$select->execute; 
while ( my($PR_ID,$NAME,$LASTNAME) = $select->fetchrow_array )
{
  $insert->execute($PR_ID,$NAME,$LASTNAME,'NULL','NULL');
}

如果源表和目标表中的列不匹配,请使用以下语法-

insert into table1(col1,col2,col3) select col1,col2,col3 from table2;

Zafar's answer is probably the best and quickest way to do this. Zafar的答案可能是做到这一点的最好,最快的方法。

But if you do want to do this row by row, one general rule for database access in a script is: never access a column without explicitly naming it. 但是,如果您确实希望逐行执行此操作,则脚本中数据库访问的一个一般规则是: 切勿在未明确命名的情况下访问列。 So: 所以:

  • No SELECT * 没有SELECT *
  • No INSERT INTO table_name VALUES (...) INSERT INTO table_name VALUES (...)

A corrected version of your script, guessing the column names: 脚本的更正版本,猜测列名称:

my $select = $dbh1->prepare("SELECT pr_id, name, lastname FROM person");
my $insert = $dbh2->prepare("INSERT INTO PERSON(pr_id, name, last_name) VALUES (?,?,?)");

$select->execute; 
while ( my($PR_ID,$NAME,$LASTNAME) = $select->fetchrow_array )
{
  $insert->execute($PR_ID,$NAME,$LASTNAME);
}

(Note that you don't need to specify the extra columns in the new table, as long as they are nullable, and you want the value to be NULL. If you do want to insert a NULL yourself, don't use 'NULL' , but use undef , like ikegami says.) (请注意,您不需要在新表中指定多余的列,只要它们可以为空,并且您希望该值为NULL即可。如果您自己想插入NULL,请不要使用'NULL' ,但请使用ikegami所说的undef 。)

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

相关问题 MYSQL:如何将整行从一个表复制到 mysql 中的另一个表,第二个表有一个额外的列? - MYSQL: How to copy an entire row from one table to another in mysql with the second table having one extra column? 从一个链接表插入行到另一个 - Insert row from one linked table to another 将一个表的每一列作为一行插入到另一个表中 - Insert each column of one table as a row in another table 从一个表中复制列,然后插入另一个表中 - copy column from one table and insert in another 创建触发器以在创建新行时将主键从一个表插入到 MySQL 中另一个表的列中 - Create trigger to insert primary key from one table upon creation of new row, into column of another table in MySQL 从一个表插入到另一个表(具有附加值)(MySQL) - Insert from one table into another (with extra values) (MySQL) 将表中的许多行插入另一表中的唯一行 - Insert many rows from a table into one unique row in another table 使用触发器将新行从另一表插入到一个表? - Use trigger to INSERT new row to one table from another table? INSERT从一个表到另一个表的列的行块 - INSERT block of rows from a column from one table to another table 将表中的一行复制到另一行,并在同一查询中插入一列的值 - copy one row from a table to another and insert value of one column in same query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM