1

我的旧表有大约 600 列。我们称这张桌子为old_table

我的新表有 603 列。我们称这张桌子为new_table

现在要将数据从old_tableinto 导入new_table,我尝试了以下方法:

$q = mysql_query("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS 
              WHERE TABLE_SCHEMA = 'databasename' AND TABLE_NAME = 'old_table'");

$cols = array();
while($r = mysql_fetch_array($q))
{   
   $cols[] = $r['COLUMN_NAME'];     
}

$sql = "INSERT INTO new_table ";
$sql .= "SELECT ";
$sql .= implode(", ", $cols);
$sql .= " FROM old_table";

mysql_query($sql) or die("Something went wrong: ".mysql_error());

但这是Column count doesn't match value count at row 1因为查询还考虑了 3 个新列new_table

有没有办法让它跳过中的附加列new_table并插入所有数据old_table

4

1 回答 1

1

是的。您可以为新列提供默认值并将其添加到您的插入语句中,或者您可以命名要插入的列。

$sql = "INSERT INTO new_table ";
$sql .= "SELECT ";
$sql .= implode(", ", $cols);
$sql .= ", value_for_new_column_1";
$sql .= ", value_for_new_column_2";
$sql .= ", value_for_new_column_3";
$sql .= " FROM old_table";

$sql = "INSERT INTO new_table ";
$sql .= "(" . implode(", ", $cols) . ")";
$sql .= "SELECT ";
$sql .= implode(", ", $cols);
$sql .= " FROM old_table";
于 2013-11-07T09:05:05.610 回答