Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 909
sharif2008 Avatar asked Jun 13 '13 04:06

sharif2008


1 Answers

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');
like image 82
Gautam3164 Avatar answered Sep 17 '22 15:09

Gautam3164