2012-08-09 82 views
0

我試試這個代碼如何從不同的表格(字段x)中選擇所有字段?笨

function catstore($id) 
    { 
     $this->db->distinct(); 
     $this->db->select('store_id'); 
     $this->db->from('products'); 
     //$this->db->join('products','products.store_id = products.store_id'); 
     $this->db->where('cat_id', $id); 
     $result = $this->db->get(); 
     return $result->result();  
    } 

它只產生不同STORE_ID,但我想用基於STORE_ID不同行的表的所有列

推薦我的任何選項提前

謝謝

回答

0

完全可以省略select函數。如果您從表中選擇全部(*),則不需要使用此功能。如果省略,笨假設你想SELECT *

http://codeigniter.com/user_guide/database/active_record.html#select

或..

您可以通過逗號分隔值select功能。因此,通過要選擇

function catstore($id) 
    { 
     $this->db->distinct(); 
     $this->db->select('store_id, column1, column2, cloumn3'); 
     $this->db->from('products'); 
     //$this->db->join('products','products.store_id = products.store_id'); 
     $this->db->where('cat_id', $id); 
     $result = $this->db->get(); 
     return $result->result();  
    } 
+0

無法正常工作..... – 2012-08-09 12:45:00

1

用逗號分隔的所有列名,您可以嘗試使用GROUP_BY

function catstore($id) 
{ 
    $this->db->select('*'); 
    $this->db->from('products'); 
    //$this->db->join('products','products.store_id = products.store_id'); 
    $this->db->where('cat_id', $id); 
    $this->db->group_by('store_id'); 
    $result = $this->db->get(); 
    return $result->result();  
} 

我知道這個問題已經是很久以前的事,但有可能會尋找相同像我這樣的問題,這就是我解決問題的方法。

相關問題