Reza Saadati
Reza Saadati

Reputation: 5419

How to clone a table to another database?

I would like to clone a table from database1 to database2. This is what I have:

$sTablename = $this->input->post('table_name', true);
$sender_table = $sTablename;
$receiver_table = $sTablename . 'xxx';

$this->Connection_model->get_custom_db('receiver')->query("CREATE TABLE $receiver_table LIKE $sender_table");

This would copy the table into the same database. But how do I copy it to another database?

I have 2 databases and I call them with:

$this->Connection_model->get_custom_db('receiver') 
$this->Connection_model->get_custom_db('sender')

Both return objects.

Upvotes: 2

Views: 164

Answers (1)

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

This might work for you

USE db2;

CREATE TABLE table2 LIKE db1.table1;

INSERT INTO table2  
   SELECT * FROM db1.table1;

Upvotes: 1

Related Questions