2011-07-14 25 views
7

total_time_driven_at_this_trip için en yüksek değere sahip benzersiz kullanıcı kimliğini almak ve user_name'u user_id numaralı ilişkiye ait olan başka bir tablodan almak için DISTINCT'u nasıl kullanırım? Cakephp DISTINCT

Bu çalıştı

...

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

ama işe yaramıyor. Doğru kesişimi olan

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

cevap

8
i Cevabınız soruya ilişkilidir sanmıyorum
// see below url 

    http://book.cakephp.org/1.3/view/1018/find 


array(
    'conditions' => array('Model.field' => $thisValue), //array of conditions 
    'recursive' => 1, //int 
    'fields' => array('Model.field1', 'DISTINCT Model.field2'), //array of field names 
    'order' => array('Model.created', 'Model.field3 DESC'), //string or array defining order 
    'group' => array('Model.field'), //fields to GROUP BY 
    'limit' => n, //int 
    'page' => n, //int 
    'offset'=>n, //int 
    'callbacks' => true //other possible values are false, 'before', 'after' 
) 



// or try this 



function some_function() { 

    $total = $this->Article->find('count'); 

    $pending = $this->Article->find('count', array('conditions' => array('Article.status' => 'pending'))); 

    $authors = $this->Article->User->find('count'); 

    $publishedAuthors = $this->Article->find('count', array(
    'fields' => 'DISTINCT Article.user_id', 
    'conditions' => array('Article.status !=' => 'pending') 
    )); 

} 
+0

- hiç – mark

1

Ben aşağıda almak gerekir herhalde

....

$this->set('tripNsws', $this->TripNsw->find('all',array('fields'=>'DISTINCT TripNsw.user_id'))); 
7

cakephp'de DISTINCT için doğru sözdizimi

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

alanlar dizide emin DISTINCT kullanımlarını olun.

0

Hep olduğu gibi DISTINCT aynı sonucu elde etmek için sorguda bir GROUP BY kullandım

0
'fields' => array('DISTINCT Model.Modelfield') 

veya

'fields' => array('Model.id','DISTINCT Model.Modelfield')