繁体   English   中英

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

[英]Select only unique values from a column in 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()不会将任何列作为参数。 它根据选择来区分结果。 我怎样才能做到这一点?

试试这样吧

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

您应该使用group_by代替distinct。

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

希望这可以帮助。

$this->db->select('column names');
$this->db->distinct();
$query = $this->db->get('table name');
$query->result();
$this->db->select('*');
        $this->db->group_by('column_name');
        $this->db->from('tbl_name');
        $query = $this->db->get();
        return $query->result();

尝试这个

   $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;
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM