Cakephp DISTINCT

Viewed 25040

How do I use DISTINCT to get unique user id with highest value for total_time_driven_at_this_trip and also pull user_name from another table which has belongsto relations based on user_id?

I tried this...

$this->set('tripNsws', $this->TripNsw->find('all',array('limit' => 20,'fields' => array('DISTINCT(TripNsw.user_id)','TripNsw.total_time_driven_at_this_trip'),'group' => array('TripNsw.user_id') ,'order' => array('TripNsw.total_time_driven_at_this_trip desc'))));

but it's not working.

I suppose you need to get below....

SELECT DISTINCT(user_id),`total_time_driven_at_this_trip` FROM `trip_nsws` order by `total_time_driven_at_this_trip` desc 
6 Answers

Correct Syntax for DISTINCT keywork in cakephp

$this->set('banners', $this->Banner->find('all',array('fields'=>'DISTINCT Banner.id')));

Make sure DISTINCT uses in fields array.

Related