Friday, August 31, 2012

Re: How to do a DISTINCT find but also return more than 1 field

I thought I'd post back with my solution.  I seem to magically figure it out 2 minutes after posting, but never before hand haha. 

I just used the 'group' key instead and that seemed to do the trick returning only distinct IP rows while bringing back data for all fields that I needed:

$DistinctIp = $this->Log->find('all', array(
'conditions'=>array(
'last_usage_human >'=>date('Y-m-d H:i:s', strtotime('-15 minutes'))
),
'fields'=>array('Log.ip', 'Log.user_agent', 'Log.ip_name', 'Log.last_usage_human'),
'limit'=>333,
                        'group'=>'Log.ip'
));

On Friday, August 31, 2012 6:32:20 PM UTC-7, andrewperk wrote:
Hello,

When trying to do a DISTINCT find with only 1 field being returned everything works fine. But if I want more than just that 1 field of data returned it's no longer distinct. How can I do this?

$DistinctIp = $this->Log->find('all', array(
'conditions'=>array(
'last_usage_human >'=>date('Y-m-d H:i:s', strtotime('-15 minutes'))
),
'fields'=>array('DISTINCT Log.ip', 'Log.user_agent', 'Log.ip_name', 'Log.last_usage_human'),
'limit'=>333
));

This does not work, it's returning everything, it's no longer DISTINCT. 

Here you can see I want only rows with the DISTINCT ip field to be returned. But I want the data that's returned from more than just that field such as the: ip_name, last_usage_human fields as well. But when I do this it returns everything and doesn't constrain it to the DISTINCT.

Thanks,


Andrew

--
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate