2011-07-14 6 views
7

total_time_driven_at_this_tripの値が一意の一意のユーザーIDを取得するのにuser_idに基づいて属している別のテーブルから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')))); 

...これを試してみましたが、それは働いていません。正しいsyntaxisがある

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

私はあなたが以下を取得する必要があるとし

....

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

cakephpのDISTINCTキーワークに関する正しい構文

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

フィールド配列にDISTINCTが使用されていることを確認します。

0

は、私はいつものようにDISTINCT同じ結果を得るために、クエリでGROUP BYを使用しました

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

または

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