从codeigniter中的列中仅选择唯一值

时间:2013-06-13 04:27:49

标签: php mysql codeigniter

我有一张这样的表

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

这里的所有行都是不同的。如果我使用

 $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()不会将任何列作为参数。它根据选择来区分结果。我怎样才能做到这一点?

5 个答案:

答案 0 :(得分:8)

试试这个

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

按名称选择不同的值。您的SELECT拼写错误,可能是打字错误。您还可以使用GROUP BY

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

答案 1 :(得分:2)

您应该使用group_by代替distinct。

因为distinct将返回唯一的行。但要逐行排列,你应该分组。

希望这有帮助。

答案 2 :(得分:1)

$this->db->select('column names');
$this->db->distinct();
$query = $this->db->get('table name');
$query->result();

答案 3 :(得分: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;
   }

答案 4 :(得分:0)

{{1}}

试试这个