CodeIgniter Active Record从一个表插入到另一个表 [英] CodeIgniter Active Record insert from one table to another

查看:73
本文介绍了CodeIgniter Active Record从一个表插入到另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用codeigniter活动记录语法将数据从一个表插入到另一个表的语法是什么?我尝试了通常的mysqli查询,但是它可以工作,但是我想使用CodeIgniter Active Record语法来保持一致性.

What is the syntax for inserting data from one table to another table, using codeigniter active record syntax? I tried the usual mysqli query and it works, but I want to use CodeIgniter Active Record syntax for consistency.

我尝试使用这些CodeIgniter Active Record查询,但还是没有运气:

I tried playing with these CodeIgniter Active Record queries but still no luck:

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

推荐答案

我认为要做的最好的事情是使用get方法从一个表中获取所需的数据,然后使用查询结果获取程序函数之一(例如result()),使用insert()方法一个一行地遍历行.

I think the best thing to accomplish that is fetching the data you want from one table using the get method, then using one of the query results grabber functions (like result() ), iterate over the rows one by one using the insert() method.

将其放入代码中

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

当然,我想table1具有与table2完全相同的结构(每列相同的列名和数据类型).如果不是这种情况,则必须使用分配将一个表中的列映射到另一个表,但是如果那样的话,您的代码将更宽.

Of course, i suppose that table1 has exactly th same structure as table2 (the same column names and data types for each column). If that is not the case, you will have to map the columns from one table to the another using assignments, but if that is the case your code will be more wide.

这篇关于CodeIgniter Active Record从一个表插入到另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆