4

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'

$this->db->query メソッドを使用せずに CodeIgniter でこれは可能ですか?

解決策:

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

よろしく

4

4 に答える 4

11

SELECT ... INSERT クエリについて話していると思いますが、アクティブなレコード クラスにはそれを行う方法はありませんが、それを行う方法は 2 つあります。

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 */
于 2010-07-29T12:56:08.603 に答える
-2
$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

于 2010-07-29T08:48:51.403 に答える