如何在cakephp2的条件下使用where和group?

时间:2014-09-30 03:35:48

标签: php sql cakephp-2.0

我是cakephp2的新手,我想问你一点帮助。我目前正在学习cakephp2,但我很难学会如何使用where和groupby子句cakephp2。 我想将下面的sql查询转换为cakphp2,但我将如何使用find()?

select  params,count(params) from pv_logs
where  dt = '2014/9/25' and is_crawler = 0
group by params order by  count(params) desc limit 100 ;

这就是我在cakephp2格式中的做法,但你觉得这有什么不对吗?

  $pvcount = $this->PvLog->find('all', array(
          'fields' => array('dt','params','count(params)'),
          'conditions'=>array('PvLog.dt'=>'2014/9/25','PvLog.is_crawler'=>0),
          'group'=>array('PvLog.params'),
          'order'=>array('PvLog.count(params)'),
          'limit' => 100,
          ));

1 个答案:

答案 0 :(得分:0)

尝试以下

 $this->PvLog->virtualFields['params_count']='count(params)';
 $pvcount = $this->PvLog->find('all', array(
      'fields' => array('dt','params','params_count'),
      'conditions'=>array('PvLog.dt'=>'2014-09-25','PvLog.is_crawler'=>0),
      'group'=>array('PvLog.params'),
      'order'=>array('params_count'=>'asc'),
      'limit' => 100,
      ));