I’m using below query to get the unique result for receiver_id but also trying to get the highest id returned only rather than lowest one which is what below find query is generating. Is there a way we can change the find optional parameters to get highest id returned only?
$this->loadModel('Chat');
$lastChat = $this->Chat->find('all', array(
'conditions' => array(
'Chat.user_id' => $user_id['User']['id']
),
'order' => array('Chat.id DESC'),
'fields' => array(
'Chat.id',
'Chat.chat',
'Chat.user_id',
'Chat.receiver_id',
'Chat.read',
'Chat.created'
),
'group' => array('Chat.receiver_id')
));
I would say it only makes sense to use a
GROUP BYclause in SQL if you are going to use an aggregate function in theSELECTclause.Maybe I’m not understanding the nature of your problem, but would this query work for you?
If that’s the case you just need to add an extra element in the
fieldsarray: