有一个大表,我需要将它的内容移动到一个具有不同结构的表中。这些表位于不同的数据库中。为此,我正在使用 PHP 脚本。但是脚本不能按照我想要的方式工作。它过度复制并且永不停止。也许这是一个笨拙而简单的问题,但现在我的头在旋转,因为尝试但我无法解决问题。而且这项工作需要立即完成。如果你能帮忙,我会很高兴。这里是代码片段:
function copy_table()
{
$this->load->database();
$num_rows = $this->db->get('orj_table')->num_rows();
$offset = 0;
$limit = 500;
while ($offset <= $num_rows)
{
$this->load->database();
//Query for original table
//......
$this->db->limit($limit, $offset);
$records = $this->db->get('orj_table')->result_array();
$this->db->close();
//Open a connection to new database.
$this->db_new = $this->load->database('new', TRUE);
foreach($records as $record)
{
$data1 = $record['data1'];
$data2 = $record['data2'];
$datas[] = array('data1 => $record['data1'],
'data2 => $record['data2']
);
}
//Insert 500 records at one time with "insert_batch"
$sorgu = $this->db_yeni->insert_batch('new_table', datas);
$this->db->close();
$offset += 500;
}
}
最佳答案
尝试使用这个简单的 mysql 查询:
INSERT INTO different_table(从 initial_table 中选择 col1、col3、col4)
关于php - 有效地将大表复制到不同的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7913782/