在笨

2013-06-13 45 views
3

選擇從一列只有唯一值我有這樣在笨

name|subjects|location 
......................  
BUET|CSE|Dhaka 
BUET|EEE|Dhaka 
RUET|CE |Rajshahi 
RU |CE |Rajshahi 

表這裏所有行distinct.And如果我使用

$this->db->select('*') and $this->db->distinct() 

,將選擇的所有行BUET但我只想要這樣

name|subjects|location 
......................  
BUET|CSE|Dhaka 
RUET|CE |Rajshahi 
RU |CE |Rajshahi 

這意味着只有第一列必須是不同的,並選擇所有的列作爲我們如果我使用$ this-> db-> select('name')和$this->db->distinct(),它會起作用。那麼關於其他專欄的內容呢?

由於我的原始表有很多列,所以我想使用$this->db->select('*')。我認爲$this->db->distinct()不會將任何列作爲參數。它基於select來區分結果。我怎樣才能做到這一點?

+0

使用'DISTINCT'查詢。 –

回答

7

嘗試這樣

$this->db->select('DISTINCT `name`'); //You may use $this->db->distinct('name'); 
$this->db->select('*'); 

通過名稱選擇不同的值。而你的SELECT拼錯了,可能是它的一個打字mistake.And您還可以使用GROUP BY像

$this->db->select('*'); 
$this->db->group_by('name'); 
+0

does $ this-> db-> distinct('name');參數??我不知道 –

+0

CI文檔中沒有參數:( –

+0

你可以使用$ this-> db-> select('DISTINCT'name''); – Gautam3164

2

您應該使用group_by代替distinct。

因爲不同將返回唯一的行。但要有獨特的行,你應該做分組。

希望這會有所幫助。

+0

謝謝。這就是我真正想要的。 –

0
$this->db->select('column names'); 
$this->db->distinct(); 
$query = $this->db->get('table name'); 
$query->result(); 
0
$this->db->select('job,id'); 
    $this->db->group_by('job'); 
    $query= $this->db->get('technicals')->result_array(); 
    if(!empty($query)){ 
     return $query; 
    }else{ 
     return false; 
    } 
+0

這段代碼只返回一個作業,來自重複作業的同名作品像(不同)其中技術是我的表名和id,job是這個表中的列 –

0
$this->db->select('*'); 
     $this->db->group_by('column_name'); 
     $this->db->from('tbl_name'); 
     $query = $this->db->get(); 
     return $query->result(); 

試試這個