CodeIgniter: How To Do a Select (Distinct Fieldname) MySQL Query

By : Ian

I'm trying to retrieve a count of all unique values in a field.

Example SQL:

SELECT count(distinct accessid) FROM (`accesslog`) WHERE record = '123'

How can I do this kind of query inside of CodeIgniter?

I know I can use $this->db->query(), and write my own SQL query, but I have other requirements that I want to use $this->db->where() for. If I use ->query() though I have to write the whole query myself.

By : Ian


You can also run ->select('DISTINCT `field`', FALSE) and the second parameter tells CI not to escape the first argument. With the second parameter the output would be SELECT DISTINCT `field` instead of without the second parameter, SELECT `DISTINCT` `field`

By : jonwayne

$record = '123';



$this->db->where('record', $record); 

$query = $this->db->get('accesslog');



should go a long way towards it.

try it out with the following code

function fun1()  
   $this->db->where('record =','123');  
   return $query->num_rows();  
By : Jasmine

This video can help you solving your question :)
By: admin