title: Mysql query SELECT DISTINCT month/year
body: <p>I am struggling to figure the correct mysql query for this one...</p>
  
  <p>I have a table named 'blog' with blog entries in it. Each entry has a datetime column (YYYY-MM-DD HH:MM:SS) named 'created.' I'd like to list on my page's sidebar all the distinct month-years in which an entry was written so the user can easy find older entries from the blog archive.</p>
  
  <p>I have a user-defined function that my BlogEntry class uses to query the database. Based on the query, it returns an array of records. For example...</p>
  
  <pre><code>$entries = BlogEntry::find_by_sql("SELECT * FROM blog ORDER BY created DESC");
  </code></pre>
  
  <p>What query can I use to return only the distinct month-years where an entry was written?</p>
  
  <p>I've tried a few things (with no luck) such as:</p>
  
  <pre><code>$months = BlogEntry::find_by_sql("SELECT DISTINCT MONTH(created) FROM blog");
  </code></pre>
  
  <p>Any ideas or suggestions on how to return the distinct months and years would be much appreciated!</p>
  
  <p>UPDATE:
  Perhaps my issue is in how to display the returned records. I've been trying the following...</p>
  
  <pre><code>&lt;ul&gt;
  &lt;?php foreach($months as $month): ?&gt;
  &lt;li&gt;&lt;?php echo $month-&gt;created; ?&gt;&lt;/li&gt;
  &lt;?php endforeach; ?&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>I know this doesn't look right. How could I formate it to list Month-YYYY ?</p>