Doing a DISTINCT select with CakePHP

To select DISTINCT records from a database, simply use the field selector attribute of a normal findAll query.

I.e. to select a grouping of years from a date column (I needed to do this for a summary page I was working on) the syntax is simply:

$this->Model->findAll(null, array(‘DISTINCT year(dateField) as yearGroup’));

Note that DISTINCT must be written in capital letters or you will get an unknown column error.

This will return an array containing the years of all the dates in your table.

Bookmark and Share

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)