Cakephp DISTINCT

前端之家收集整理的这篇文章主要介绍了Cakephp DISTINCT前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何使用DISTINCT为total_time_driven_at_this_trip获取具有最高值的唯一用户ID,还从另一个基于user_id的属于关系的表中提取user_name?

我试过这个……

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

但它不起作用.

我想你需要得到以下……

  1. SELECT DISTINCT(user_id),`total_time_driven_at_this_trip` FROM `trip_nsws` order by `total_time_driven_at_this_trip` desc
  1. // see below url
  2.  
  3. http://book.cakePHP.org/1.3/view/1018/find
  4.  
  5.  
  6. array(
  7. 'conditions' => array('Model.field' => $thisValue),//array of conditions
  8. 'recursive' => 1,//int
  9. 'fields' => array('Model.field1','DISTINCT Model.field2'),//array of field names
  10. 'order' => array('Model.created','Model.field3 DESC'),//string or array defining order
  11. 'group' => array('Model.field'),//fields to GROUP BY
  12. 'limit' => n,//int
  13. 'page' => n,//int
  14. 'offset'=>n,//int
  15. 'callbacks' => true //other possible values are false,'before','after'
  16. )
  17.  
  18.  
  19.  
  20. // or try this
  21.  
  22.  
  23.  
  24. function some_function() {
  25.  
  26. $total = $this->Article->find('count');
  27.  
  28. $pending = $this->Article->find('count',array('conditions' => array('Article.status' => 'pending')));
  29.  
  30. $authors = $this->Article->User->find('count');
  31.  
  32. $publishedAuthors = $this->Article->find('count',array(
  33. 'fields' => 'DISTINCT Article.user_id','conditions' => array('Article.status !=' => 'pending')
  34. ));
  35.  
  36. }

猜你在找的PHP相关文章