Recent Posts
- (02/24) Calling an Element from a Helper TAGS:CakephpWeb ProgrammingUsageTutorialExample
- (10/14) Forcing A Single Join in CakePHP PaginationTAGS:CakephpCakephp 13DatabaseErrorsHabtmMysqlMssqlWeb ProgrammingUsage
- (08/11) Inserting NOW() into MySQL Using CakePHPTAGS:CakephpDatabaseMysqlMssqlWeb ProgrammingUsage
- (08/07) Best PaginateCount for CakePHP - with Group By SupportTAGS:CakephpBehaviorsWeb ProgrammingUsageDatabaseExampleMysqlHabtmHack
- (04/03) CakePHP Error Messages Not Showing on FormTAGS:CakephpCakephp 13Web ProgrammingErrorsUsage
- (02/06) Sorting Paginated Results Using a Related Model Field in CakePHPTAGS:CakephpWeb ProgrammingUsageExample
- (11/02) Changing CakePHP's Model useTable on the FlyTAGS:CakephpWeb ProgrammingMysqlDatabaseExampleTutorial
- (10/18) The Funniest Error Message Ever - Thank You EclipseTAGS:EclipseWeb ProgrammingErrorsHumorWindowsUsageSoftwarePhpCakephp
Subscribe to my feed
MainelyDesign.com Blog
Distinct vs. Group By in MySQL (and CakePHP)
Posted on 05/21/2009 at 11:27 am by Kevin Wentworth
Viewed 6,161 times | 3 comments
An interesting problem- I am returning a list of files that have already been uploaded, so the user can link to an existing file. This creates a new record (duplicate record). The next time the user goes to select a file, there will now be two entries for the same file. DISTINCT to the rescue, or so I thought.
DISTINCT applies to the whole select statement
This is confusing in its own right. The CakePHP book misleadingly demonstrates that you can setup the fields parameter like so:
I tried this and a MySQL error kept getting returned. I switched over to mysql and ran the query until it worked. Turns out that the DISTINCT keyword has to come first in the SELECT statement, and it applies to the whole select statement (e.g. same category, but different name value makes it distinct). I'm not sure really how to get CakePHP to output the correct query using the DISTINCT modifier- I think I would start the fields array() with DISTINCT... let me know if you figure it out.
GROUP BY applies only to the field(s) specified
The GROUP BY command takes a different approach and essentially "groups" all results by the fields you specify and returns a single instance of each unique value (what I wanted). The code is simple:
This returned exactly what I needed. Group By is the way to go, I think, in most cases. The only time DISTINCT is really good, is for returing all the distinct values of a single column.
To Recap
- DISTINCT applies to the entire SELECT statement (DISTINCT across all fields listed, not just 1 field if more than 1 are listed)
- GROUP BY applies to only the specified fields (a distinct value for the group by field only, doesn't factor in other fields being returned)
- For most cases use GROUP BY if you are returning more than 1 field and want distinct values for a particular column in the field list
Thanks to this simple example. Cheers,
-Kevin Wentworth
Tags for Distinct vs. Group By in MySQL (and CakePHP)
Cakephp | Web Programming | Mysql | Usage
Comments for this Posting
Posted by Marco
on 12/1/11
I came across this searching for Distinct solutions in CakePHP and thought it's worth mentioning ....
Distinct, Group By, or any other operation that forces sorting of a result set is very expensive. Why not prevent the duplicate record from being inserted in the first place so that you don't need to rely on either?
You're right that Distinct or Group By hide the problem this time, but you'll wind up chasing the same problem later on unless you fix the problem at the data level.
Just sharing.
Posted by damnedoneo
on 27/2/11
Hey, its John from USA. I found mainelydesign.com quite interesting and I would love to explore it. Hope we gonna have a good time here.
Regards
Download
Sorry, comments are closed for this posting.
Please Email Kevin if you have any questions. Thanks!

Posted by vitucho
on 16/3/10
Thanks a lot for this information. I was driving crazy with this 'distinct' issue. The 'group' strategy was very cool.
Thanks again.
http://vitublog00.blogspot.com/