Cakephp独特

Cakephp独特,php,cakephp,Php,Cakephp,我如何使用DISTINCT获取此行程中总时间驱动时间最大的唯一用户id,以及如何从另一个具有基于用户id的belongsto关系的表中提取用户名 我试过这个 $this->set('tripNsws', $this->TripNsw->find('all',array('limit' => 20,'fields' => array('DISTINCT(TripNsw.user_id)','TripNsw.total_time_driven_at_this_trip'

我如何使用
DISTINCT
获取此行程中
总时间驱动时间最大的唯一用户id
,以及如何从另一个具有基于
用户id
的belongsto关系的表中提取
用户名

我试过这个

$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 

正确的联会是

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

可以考虑将其设置为虚拟字段()


在cakephp中为不同的键工作更正语法

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

确保在字段数组中使用DISTINCT。

我一直在查询中使用GROUP BY来获得与DISTINCT相同的结果

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


我认为你的答案与这个问题毫无关系
'fields' => array('DISTINCT Model.Modelfield')
'fields' => array('Model.id','DISTINCT Model.Modelfield')
// 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')
    ));

}