按 ID 分组和按日期排序
$lastComments = $this->Comment->find('all', array('fields' => array('Comment.news_id', 'Comment.date', 'Comment.content'),
'group' => array('Comment.news_id, Comment.date'),
'order' => array('Comment.date DESC'))
);
这个想法是从独特的主题(1 条评论 - 1 个主题)获取最新评论。
此代码不处理唯一 ID(唯一主题),我该如何解决这个问题?
独特是行不通的。
$lastComments = $this->Comment->find('all', array('fields' => array('Comment.news_id', 'Comment.date', 'Comment.content'),
'group' => array('Comment.news_id'),
'order' => array('Comment.date DESC'))
);
此代码将返回唯一的主题,但按日期排序不起作用:/
$lastComments = $this->Comment->find('all', array('fields' => array('Comment.news_id', 'Comment.date', 'Comment.content'),
'group' => array('Comment.news_id, Comment.date'),
'order' => array('Comment.date DESC'))
);
The idea is to get latest comment from unique topics (1 comment - 1 topic).
This code doesn't handle unique ID's (unique topics), how Can I fix that?
Distinct doesn't work.
$lastComments = $this->Comment->find('all', array('fields' => array('Comment.news_id', 'Comment.date', 'Comment.content'),
'group' => array('Comment.news_id'),
'order' => array('Comment.date DESC'))
);
This code will return unique topics but order by date doesn't work :/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想这会对你有一点帮助......试试这个
I think this will be help you a bit...Just try this
你提到 DISTINCT 不起作用。您在 CakePHP 中使用 DISTINCT 语法是否遇到问题,或者正确使用 DISTINCT 没有产生正确的结果?我会尝试:
You mentioned DISTINCT didn't work. Did you have trouble using the DISTINCT syntax in CakePHP, or did using DISTINCT correctly not yield the correct results? I'd try:
如果它可以帮助的话,我在没有 cakephp 的情况下遇到了同样的问题,解决方案是使用 max(date)
Prehaps 中的类似 cakephp 的东西:
If it can help, I had the same problem without cakephp, the solution was to use a max(date)
Prehaps something like that in cakephp :