Select only unique values from a column in codeigniter

老子叫甜甜 提交于 2020-01-03 14:24:08

问题


i have a table like this

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

here all the rows are distinct.And if I use

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

it would select all the rows of BUET but i only want like this

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

That means only the first column must be distinct and select all the columns as usual.And it would work if i use $this->db->select('name') and $this->db->distinct(). Then what would be about the other columns??

As my original table has many columns so I want to use $this->db->select('*'). I think $this->db->distinct() does not take any column as parameter. It differentiate result based on select. How can I do this?


回答1:


Try like this

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

Select the distinct values by names.And your SELECT spelt wrong,may be its a typing mistake.And you can also use GROUP BY like

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



回答2:


You should use group_by in place of distinct.

because distinct will return unique rows. but to have unique column by row you should do group by.

Hope this helps.




回答3:


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



回答4:


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

try this




回答5:


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


来源:https://stackoverflow.com/questions/17079243/select-only-unique-values-from-a-column-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!