[英]How to INNER JOIN 3 tables using CodeIgniter
有人能告訴我如何用PHP加入3表嗎? 例
SELECT FROM table1, table2,table on INNERJOIN -------------------
讓我有3個表。(問題表,答案表和類別表)這是我的網頁的示例。
Time remaining 30 minutes(I will get "30 minutes" form Category table)
1. Question (from question table)
2. answer (from answer table)
我不知道如何加入3表。
它應該是那樣的,
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.id');
$this->db->join('table3', 'table1.id = table3.id');
$query = $this->db->get();
根據CodeIgniters活動記錄框架
我相信使用CodeIgniters活動記錄框架,您將一個接一個地使用兩個連接語句。
例如:
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table1', 'table1.id = table2.id');
$this->db->join('table1', 'table1.id = table3.id');
$query = $this->db->get();
嘗試一下,看看它是怎么回事。
我認為在CodeIgniter中最好使用ActiveRecord,如上所述。 還有一件事:您可以在AR中使用方法鏈接:
$this->db->select('*')->from('table1')->join('table2','table1.id=table2.id')->...
我創建了一個函數來獲取一個包含字段值和連接的數組。 這在模型中:
public function GetDataWhereExtenseJoin($table,$fields,$data) {
//pega os campos passados para o select
foreach($fields as $coll => $value){
$this->db->select($value);
}
//pega a tabela
$this->db->from($table);
//pega os campos do join
foreach($data as $coll => $value){
$this->db->join($coll, $value);
}
//obtem os valores
$query = $this->db->get();
//retorna o resultado
return $query->result();
}
這在控制器中:
$data_field = array(
'NameProduct' => 'product.IdProduct',
'IdProduct' => 'product.NameProduct',
'NameCategory' => 'category.NameCategory',
'IdCategory' => 'category.IdCategory'
);
$data_join = array
( 'product' => 'product_category.IdProduct = product.IdProduct',
'category' => 'product_category.IdCategory = category.IdCategory',
'product' => 'product_category.IdProduct = product.IdProduct'
);
$product_category = $this->mmain->GetDataWhereExtenseJoin('product_category', $data_field, $data_join);
結果:
echo '<pre>';
print_r($product_category);
die;
你可以像這樣修改你的編碼
$this->db->select('a.nik,b.nama,a.inv,c.cekin,c.cekout,a.tunai,a.nontunai,a.id');
$this->db->select('DATEDIFF (c.cekout, c.cekin) as lama');
$this->db->select('(DATEDIFF (c.cekout, c.cekin)*c.total) as tagihan');
$this->db->from('bayar as a');
$this->db->join('pelanggan as b', 'a.nik = b.nik');
$this->db->join('pesankamar_h as c', 'a.inv = c.id');
$this->db->where('a.user_id',$id);
$query = $this->db->get();
return $query->result();
我希望可以解決你的SQL
為了執行純SQL語句(我不知道FRAMEWORK- CodeignITER !!!)你可以使用SUB QUERY! 語法如下
SELECT t1.id FROM例如點t1 INNER JOIN(自(示例2選擇ID t1
加入示例3 t2
上t1
。 id
= t2
。 id
為t2 ON t1.id = t2.id));
希望你明白我的意思!
function fetch_comments($ticket_id){
$this->db->select('tbl_tickets_replies.comments,
tbl_users.username,tbl_roles.role_name');
$this->db->where('tbl_tickets_replies.ticket_id',$ticket_id);
$this->db->join('tbl_users','tbl_users.id = tbl_tickets_replies.user_id');
$this->db->join('tbl_roles','tbl_roles.role_id=tbl_tickets_replies.role_id');
return $this->db->get('tbl_tickets_replies');
}
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.id', 'inner');
$this->db->join('table3', 'table1.id = table3.id', 'inner');
$this->db->where("table1", $id );
$query = $this->db->get();
您可以在哪里指定應查看哪個ID或在特定表中選擇。 您還可以在join方法的第三個參數中選擇左,右,外,內,左外和右外的連接部分。
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.id','JOIN Type');
$this->db->join('table3', 'table1.id = table3.id');
$query = $this->db->get();
這將給你table1,table2,table3的結果,你可以在$ this-> db-> join()函數的第三個變量中使用任何類型的連接,如inner,left,right等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.