简体   繁体   中英

Select only unique values from a column in codeigniter

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?

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

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.

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

try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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