代码之家  ›  专栏  ›  技术社区  ›  vikmalhotra

如何在codeigniter活动记录中使用select插入记录

  •  4
  • vikmalhotra  · 技术社区  · 15 年前

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

    在CodeIgniter中不使用$this->数据库->查询方法?

    解决方案 :

    $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);
        }           
    }
    

    2 回复  |  直到 15 年前
        1
  •  11
  •   Edgar Nadal    15 年前

    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 */
    
        2
  •  0
  •   Vincent    4 年前

    这是一篇老文章,但可能对某些人有用。

    这和埃德加·纳达尔的答案一样,通过更安全的方式传递参数进行查询

    $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));
    

        3
  •  0
  •   Msd.Abd    4 年前

    如果您想对查询执行有很好的控制,那么您可以选择。。。以3种方式插入:

    1) 使用codeigniter活动记录插入批(ci3)或插入批(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 */}
    

    $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
  •  -2
  •   C. E.    15 年前
    $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

    推荐文章