2015-04-01 64 views
0

我正在codeigniter中工作。我的問題是我想顯示具有相同ID的所有行。但是當我執行group_by時,它只輸出該組的一行。下面在Codeigniter中選擇('*')with group_by

是我的模型

function category_content(){ 
     $this->db->select('*'); 
     $this->db->from('category'); 
     $this->db->group_by('category_id'); 
     $query = $this->db->get(); 
     return $query->result_array(); 
    } 

請幫助。

+0

,而不是由,如果你想獲得特定ID的記錄然後通過使用where子句組ID – 2015-04-01 12:22:16

+0

使用順序組使用順序是使用的時候,需要把相同的行。 – 2015-04-01 12:35:45

回答

1

根據sql屬性Group By將所有匹配的記錄分組,並只會顯示一個。看來你想按id排序。然後,它能夠更好地在你的情況下使用order by

0

參見示例希望你會了解這個..

tableA 
    _____ 
    id name marks 
    -- ---- --- 
    1 x  25 
    2 y  27 
    1 z  30 


    SELECT * FROM tableA group by id 

OUTPUT: 
1 x 25 
2 y 27 

你應該使用WHERE子句。

SELECT * FROM tableA WHERE id=1 

OUTPUT: 
1 x 25 
1 z 30 


    function category_content(){ 
      $this->db->select('*'); 
      $this->db->from('category'); 
      $this->db->where($category_id); 
      $query = $this->db->get(); 
      return $query->result_array(); 
     } 

https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html