3

I am building a query in CakePHP, but cannot use a GROUP BY statement in my find('count') as it throws an error. Therefore I used instead of the GROUP BY statement a DISTINCT field in my fieldlist.

So, from find('count', array('group' => 'field')) I now have find('count', array('fields' => 'DISTINCT field'))

In another part of the application, I am using a find('all') to fetch the same records as above. Here, I am able to use the GROUP BY statement, so I have:

find('all', array('group' => 'field'))

My question is: are both of the queries described above returning the same results? Briefly I want to be sure that DISTINCT field combined with find('count') returns the same results as GROUP BY field combined with find('all').

4
  • you can look at the generated sql query, and/or the data they return to determine that.
    – Anh Pham
    Commented Jul 26, 2011 at 23:17
  • It would be a particular case. I want to be sure that this applies in general.
    – linkyndy
    Commented Jul 26, 2011 at 23:24
  • What error did the GROUP BY throw?
    – deceze
    Commented Jul 26, 2011 at 23:32
  • A syntax error in MySQL. Don't remember exactly as I changed it to reflect what I have asked above. I only want to be sure that the two return the same results; I believe it is not CakePHP dependent, but MySQL in general.
    – linkyndy
    Commented Jul 27, 2011 at 8:37

1 Answer 1

10

An old post, but I needed help on this and eventually found the answer, so here it is.

They will be the same if you do this...

find('count', array('fields' => 'COUNT(DISTINCT field) as count'))
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.