2011-09-16 39 views
8

有人能告訴我如何加入3表與PHP? 例如何使用CodeIgniter INNER JOIN 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表。

+1

你怎麼做呢?使用硬編碼的SQL語句,使用像ActiveRecord這樣的框架?或者是其他東西? – Klee

+0

我正在使用Codeigniter。 –

回答

27

它應該是這樣的,

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

因爲我認爲CI中最好使用ActiveRecord如上寫道。 一兩件事:你可以使用方法鏈在AR:

$this->db->select('*')->from('table1')->join('table2','table1.id=table2.id')->... 
3

我相信,使用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(); 

給一個嘗試,看看它是如何去。

+0

謝謝克利,你能告訴我,如果不codeigniter?如何使用Sql命令連接三個表?對不起,我的英語不好用,請耐心等待。 –

1

對於執行純SQL語句(我不知道FRAMEWORK-CodeignITER !!!) 您可以使用SUB QUERY! 語法將如下

SELECT t1.id FROM例如點t1 INNER JOIN (選擇ID(示例2 t1t1加入示例3 t2id = t2id))爲t2 ON t1.id = t2時。ID;

希望你能得到我的觀點!每CodeIgniters活動記錄框架

+0

感謝Tirth Bodawala –

2

我創建了一個函數來獲取與字段中的值,並加入一個陣列。這正好模型:

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

在這裏你可以SPE cify應該查看哪個id或在特定表格中選擇。您還可以選擇連接方法的第三個參數的左邊,右邊,外邊,內邊,左外邊和右邊外的哪個連接部分。

1

可以modiv您的編碼這樣

$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