【问题标题】:How to insert records using select in codeigniter active record如何在codeigniter活动记录中使用select插入记录
【发布时间】:2011-03-22 14:20:26
【问题描述】:

我想使用 CodeIgniter Active Record 类实现一个 sql 查询。查询看起来像这样..

INSERT california_authors (au_id, au_lname, au_fname)
SELECT au_id, au_lname, au_fname
FROM authors
WHERE State = 'CA'

在 CodeIgniter 中是否可以不使用 $this->db->query 方法?

解决方案

$this->db->select('au_id, au_lname, au_fname');
$this->db->from('california_authors');
$this->db->where('state', 'CA');
$query = $this->db->get();

if($query->num_rows()) {
    $new_author = $query->result_array();

    foreach ($new_author as $row => $author) {
        $this->db->insert("authors", $author);
    }           
}

问候

【问题讨论】:

    标签: activerecord codeigniter batch-insert


    【解决方案1】:

    我认为您正在谈论一个 SELECT ... INSERT 查询,在活动记录类上没有方法可以做到这一点,但有两种方法可以做到这一点

    1)

    $query = $this->db->query('INSERT california_authors (au_id, au_lname, au_fname)
                               SELECT au_id, au_lname, au_fname
                               FROM authors
                               WHERE State = \'CA\'');
    

    如你所说

    而且 2) 你可以这样做,使用 Calle 所说的,

    $select = $this->db->select('au_id, au_lname, au_fname')->where('state', 'CA')>get('california_authors');
    if($select->num_rows())
    {
        $insert = $this->db->insert('california_authors', $select->result_array());
    }
    else
    { /* there is nothing to insert */
    

    【讨论】:

    • 我认为if($select->num_rows())内部也需要有一个foreach
    【解决方案2】:

    这是一篇旧帖子,但这可能对某人有用。

    和 Edgar Nadal 的回答一样,只是传递参数到查询的方式更安全

    $state = 'CA';
    $sql = "
    INSERT california_authors (au_id, au_lname, au_fname)
    SELECT au_id, au_lname, au_fname
    FROM authors
    WHERE State = ?
    ";
    $this->db->query($sql, array($state));
    

    codeigniter-3.2.1

    【讨论】:

      【解决方案3】:

      如果您想很好地控制查询执行,那么您可以通过 3 种方式执行 SELECT ... INSERT:

      1)使用codeigniter活动记录insert_batch(ci3)或insertBatch(ci4)(推荐):

      $select = $this->db->select('au_id, au_lname, au_fname')->where('state','CA')>get('california_authors');
      if($select->num_rows())
      {
          $insert = $this->db->insert_batch('california_authors', $select->result_array());
      }
      else
      { /* there is nothing to insert */}
      

      2)使用codeigniter活动记录简单插入:

      $select = $this->db->select('au_id, au_lname, au_fname')->where('state','CA')>get('california_authors');
      if($select->num_rows())
      {
         foreach($select->result_array() as $row) 
           $this->db->insert('california_authors', $row);
      }
      else
      { /* there is nothing to insert */}
      

      3)使用codeigniter活动记录查询执行:

      $query = $this->db->query('INSERT california_authors (au_id, au_lname, au_fname)
                             SELECT au_id, au_lname, au_fname
                             FROM authors
                             WHERE State = \'CA\'');
      

      【讨论】:

        【解决方案4】:
        $query = $this->db->insert('california_authors', array('au_id' => 'value', 'au_lname' => 'value', 'au_name' => 'value'));
        
        $query2 = $this->db->select('au_id, au_lname, au_fname')->where('state', 'CA')->get('california_authors');
        

        要检索结果,您可以这样做:

        $resultarr = $query->result_array(); // Return an associative array
        

        手册中有很多这方面的信息。

        http://codeigniter.com/user_guide/database/active_record.html

        【讨论】:

        • 你为什么接受这个?这显然是错误的。 Calle 正在向您展示如何完全执行两个不同的语句,而不是 INSERT...SELECT 查询。
        • 我同意,这不应该被接受,因为我误解了这个问题。埃德加纳达尔的方式就是要走的路。对不起。
        • @Phil - 我尝试了代码并意识到 Calle 提供的解决方案不起作用。
        猜你喜欢
        • 2014-12-15
        • 2011-09-28
        • 1970-01-01
        • 2012-12-06
        • 1970-01-01
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多