按 ID 分组和按日期排序

发布于 2024-11-26 22:01:20 字数 733 浏览 1 评论 0原文

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷爱 2024-12-03 22:01:20

我想这会对你有一点帮助......试试这个

$this->Post->find('all',array( 'order' => array('id DESC') )  ); 

I think this will be help you a bit...Just try this

$this->Post->find('all',array( 'order' => array('id DESC') )  ); 
浅暮の光 2024-12-03 22:01:20
$this->Comment->find('first', array('order'=>array('Comment.id DESC')));
$this->Comment->find('first', array('order'=>array('Comment.id DESC')));
碍人泪离人颜 2024-12-03 22:01:20

你提到 DISTINCT 不起作用。您在 CakePHP 中使用 DISTINCT 语法是否遇到问题,或者正确使用 DISTINCT 没有产生正确的结果?我会尝试:

$lastComments = $this->Comment->find('all', array('fields' => 
                                                    array('DISTINCT Comment.news_id', 'Comment.date', 'Comment.content'),
                                                 'order' => array('Comment.date DESC'))
    );

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:

$lastComments = $this->Comment->find('all', array('fields' => 
                                                    array('DISTINCT Comment.news_id', 'Comment.date', 'Comment.content'),
                                                 'order' => array('Comment.date DESC'))
    );
脱离于你 2024-12-03 22:01:20

如果它可以帮助的话,我在没有 cakephp 的情况下遇到了同样的问题,解决方案是使用 ma​​x(date)

Prehaps 中的类似 cakephp 的东西:

$lastComments = $this->Comment->find('all', 
array('fields' => array('Comment.news_id', 
                        'Comment.date', 
                        'Comment.content'),
                'group' => array('Comment.news_id'),    
                'order' => array('max(Comment.date) DESC'))
        );

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 :

$lastComments = $this->Comment->find('all', 
array('fields' => array('Comment.news_id', 
                        'Comment.date', 
                        'Comment.content'),
                'group' => array('Comment.news_id'),    
                'order' => array('max(Comment.date) DESC'))
        );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文