猿问

Yii2 将表从一个数据库复制到另一个数据库

在 yii2 中,我必须将整个表从一个数据库复制到另一个数据库


为此,我使用了以下代码,但出现了错误:


$dbName = "db1";

$table = "demotable";

$liveDbName = "db2";

$command3 = $connection->createCommand('CREATE TABLE `'.$dbName.'.'.$table.'` SELECT * FROM `'.$liveDbName.'.'.$table.'`');

$command3->execute();

但出现如下错误:


Database Exception – yii\db\Exception

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db1.db2.demotable' doesnt exist

The SQL being executed was: CREATE TABLE `db1.demotable` SELECT * FROM `db2.demotable`


Error Info: Array

(

    [0] => 42S02

    [1] => 1146

    [2] => Table 'db1.db2.demotable' doesnt exist

)


德玛西亚99
浏览 110回答 1
1回答

蝴蝶不菲

如果要指定带有表的数据库,则应该有两个不同的字符串并用点分隔。`database`.`table`。在您的代码中将 `'.$dbName.'.'.$table.'`返回统一的字符串`db1.demotable`。所以 SQL 试图找到一个名为“db1.demotable”的表。第二个表也有同样的问题。尝试使用此代码:$dbName = "db1";$table = "demotable";$liveDbName = "db2";$command3 = $connection->createCommand("CREATE TABLE `$dbName`.`$table` SELECT * FROM `$liveDbName`.`$table`");$command3->execute();
随时随地看视频慕课网APP
我要回答