笨活动记录从一个表到另一个刀片(CodeIgniter Active Record insert f

2019-08-08 11:06发布

什么是插入从一个表中的数据到另一个表,用笨的活动记录语法的语法? 我尝试了平时的mysqli查询和它的作品,但我想用CodeIgniter的Active Record的语法的一致性。

我试着用这些笨活动记录查询,但仍然没有运气玩:

function insert_into()  
{    
    $this->db->insert('table1');
    $this->db->set('to_column');  
    $this->db->select('from_column');
    $this->db->from('table2');
}

Answer 1:

我认为最好的办法来实现这一目标是从一个表使用GET方法获取你想要的数据,然后使用的查询结果抓取功能之一(如结果()),通过行遍历一个接一个地使用插入()方法。

在代码中把这样的:

 $query = $this->db->get('table1'); foreach ($query->result() as $row) { $this->db->insert('table2',$row); } 

当然,我假设表1具有完全相同个相同的结构,表2(对每一列相同的列名和数据类型)。 如果不是的话,你将不得不列一个表映射到另一个使用分配,但如果是这样的话你的代码会更加广泛。



Answer 2:

复制$ source_table到$ archive_table:

if(($this->db->table_exists($source_table) && ($this->db->table_exists($archive_table)){
    if ($this->db->query("CREATE TABLE $archive_table LIKE $source_table")){
        if($this->db->query("INSERT $archive_table SELECT * FROM $source_table")){
           echo 'copied ok';
        }
     } 
}

整洁比遍历整个结果集,每次插入一行。



文章来源: CodeIgniter Active Record insert from one table to another