[英]in codeigniter how do i do a join with a where clause
所以我有兩個表,我想從表1中獲取滿足where子句條件的所有行,然后根據連接條件將它們與表2連接起來。
這是示例表:
table1:
col1 col2 col3
1 a val1
2 b val2
3 c val3
table2:
col1 col3
1 someval1
2 someval2
3 someval3
現在我想獲取表1中col1 = 2的所有行,並將這些行與table2中的行連接,其中table2.col1 = table1.col1。 那有意義嗎?
自從我編寫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合作的認可;-)
嘗試這個 :
$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();
希望能幫助到你 :)
$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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.