0

codeigniter でユニオンを実行しようとしています。このコードのスケルトンを別の場所で見つけました。

 $this->db->select('title, content, date');
 $this->db->from('mytable');
 $query = $this->db->get();
 $subQuery1 = $this->db->_compile_select();

 $this->db->_reset_select();

 // #2 SubQueries no.2 -------------------------------------------

 $this->db->select('title, content, date');
 $this->db->from('mytable2');
 $query = $this->db->get();
 $subQuery2 = $this->db->_compile_select();

 $this->db->_reset_select();

 // #3 Union with Simple Manual Queries --------------------------

 $this->db->query("select * from ($subQuery1 UNION $subQuery2) as unionTable");

 // #3 (alternative) Union with another Active Record ------------

 $this->db->from("($subQuery1 UNION $subQuery2)");
 $this->db->get();

私を混乱させているのは、元のサブクエリに get_where 句があると言うのですが、それをどのように含めるのですか? このようなもの:

クエリ 1

 $this->db->select('Id,title, content, date');
 $this->db->from('mytable');
 $query = $this->db->get_where('Boo', array('Id' => $foo['foo_Id']);
 $subQuery1 = $this->db->_compile_select();

クエリ 2

  $this->db->select('title, content, date');
  $this->db->from('mytable2');
  $query = $this->db->get_where('Boo', array('Title' => $foo['foo_Title'])
  $subQuery2 = $this->db->_compile_select();

ありがとうございました。

4

2 に答える 2

1

それらをまとめて書くことができます。そのように従う必要はありません。ここに例を書きます:

$q = '
 SELECT userid 
 FROM users
 WHERE userid=10
 UNION
 SELECT userid
 FROM users
 WHERE userid=5
';
$result = $this->db->query($q);
return $result;
于 2012-04-05T13:54:17.703 に答える
0

あなたが見つけた例は、十分に文書化されていないと従うのが非常に難しいでしょう。私はそれに対してあなたに警告します。

その他のオプションは次のとおりです。

  • ビュー
  • ストアド プロシージャ
  • クエリのハードコーディング

コメンターが言及しているように、dbエンジンを切り替える可能性を節約していない限り、ハードコーディングすることをお勧めします。

覚えておいてください、常に手袋を使用してください

于 2012-04-06T02:38:09.157 に答える