7

2 つのテーブルがあり、where 句の条件を満たすすべての行をテーブル 1 から取得し、結合条件に基づいてそれらをテーブル 2 と結合したいと考えています。

サンプルテーブルは次のとおりです。

table1:

col1   col2  col3
1      a     val1
2      b     val2
3      c     val3

table2:

col1   col3
1      someval1
2      someval2
3      someval3

ここで、col1 = 2 であるテーブル 1 のすべての行を取得し、それらの行を table2.col1 = table1.col1 である table2 の行と結合します。それは理にかなっていますか?

4

3 に答える 3

18

CI を書いてからしばらく経ちましたが、このドキュメント ページによると、ソリューションは次のようになります。

$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.col1 = table2.col1');
$this->db->where('table1.col1', 2);

$query = $this->db->get();

この回答は、Code Igniterの使用を推奨するものではありません ;-)

于 2012-08-06T02:07:12.347 に答える
3

これを試して :

$this->db->select('*'); // Select field
$this->db->from('table1'); // from Table1
$this->db->join('table2','table1.col1 = table2.col1','INNER'); // Join table1 with table2 based on the foreign key
$this->db->where('table1.col1',2); // Set Filter
$res = $this->db->get();

それが役に立てば幸い :)

于 2012-08-06T02:05:56.230 に答える
0
$this->db->select('book_id, book_name, author_name, category_name');
$this->db->from('books');
$this->db->join('category', 'category.category_id = books.category_id');
$this->db->where('category_name', 'Self Development');
$query = $this->db->get();

// Produces SQL:
 select book_id, book_name, author_name, category_name from books 
 join category on category.category_id = books.category_id 
 where category_name = "Self Development"
于 2016-11-11T07:25:53.307 に答える