2011-07-14 13 views
7

मैं DISTINCT का उपयोग कैसे करूँ total_time_driven_at_this_trip के लिए सबसे अधिक मूल्य के साथ अद्वितीय प्रयोक्ता आईडी प्राप्त करने के लिए है और यह भी एक और मेज जिस पर user_id आधारित संबंधों belongsto है से user_name खींच?CakePHP DISTINCT

मैं इस कोशिश की ...

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

लेकिन यह काम नहीं कर रहा।

मैं तुम्हें नीचे प्राप्त करने की आवश्यकता लगता है ....

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

उत्तर

8
// 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

मुझे नहीं लगता है कि आपका जवाब सवाल से संबंधित है - बिलकुल – mark

1

सही Syntaxis है

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

केकफ़्पी में DISTINCT कुंजीपटल के लिए सही सिंटेक्स

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

सुनिश्चित करें कि DISTINCT फ़ील्ड सरणी में उपयोग करता है।

0

मैं हमेशा क्वेरी में एक समूह द्वारा उपयोग किया है के रूप में विशिष्ट एक ही परिणाम प्राप्त करने के लिए

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

या

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