php Cakephp 与众不同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6687482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cakephp DISTINCT
提问by Passionate Engineer
How do I use DISTINCT
to get unique user id with highest value for total_time_driven_at_this_trip
and also pull user_name
from another table which has belongsto relations based on user_id
?
如何使用DISTINCT
具有最高值的唯一用户 IDtotal_time_driven_at_this_trip
并user_name
从另一个表中提取基于 的属于关系的表user_id
?
I tried this...
我试过这个...
$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'))));
but it's not working.
但它不起作用。
I suppose you need to get below....
我想你需要在下面......
SELECT DISTINCT(user_id),`total_time_driven_at_this_trip` FROM `trip_nsws` order by `total_time_driven_at_this_trip` desc
回答by Abid Hussain
// 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')
));
}
回答by Panky
Correct Syntax for DISTINCT keywork in cakephp
cakephp 中 DISTINCT 键的正确语法
$this->set('banners', $this->Banner->find('all',array('fields'=>'DISTINCT Banner.id')));
Make sure DISTINCT uses in fields array.
确保 DISTINCT 在 fields 数组中使用。
回答by edwinallenz
The correct syntaxis is
正确的语法是
$this->set('tripNsws', $this->TripNsw->find('all',array('fields'=>'DISTINCT TripNsw.user_id')));
回答by tvdias
You can consider setting it as a Virtual Field ( http://book.cakephp.org/1.3/view/1608/Virtual-fields)
您可以考虑将其设置为虚拟字段(http://book.cakephp.org/1.3/view/1608/Virtual-fields)
$this->Model->virtualFields['distinct_user'] = 'DISTINCT Model.user_id';
回答by Martin Fraser
I've always used a GROUP BY in the query to get the same result as DISTINCT
我一直在查询中使用 GROUP BY 来获得与 DISTINCT 相同的结果
回答by 001221
'fields' => array('DISTINCT Model.Modelfield')
or
或者
'fields' => array('Model.id','DISTINCT Model.Modelfield')