2011-07-28 67 views
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和按日期

此代碼不處理唯一的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')) 
     ); 

此代碼將返回唯一的主題,但爲了通過日期不工作:/

回答

0

你提到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')) 
    ); 
+0

我試過之前我貼:/ 有沒有其他想法? 感謝您的回覆。 乾杯。 – JaRo

-1

如果它可以幫助,我有沒有CakePHP的同樣的問題,解決方法是使用一個最大(日)

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

我認爲這將是你有點幫助...只是試試這個

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