Skip to content

Instantly share code, notes, and snippets.

@jiripudil
Created October 13, 2012 09:10
Show Gist options
  • Save jiripudil/3883912 to your computer and use it in GitHub Desktop.
Save jiripudil/3883912 to your computer and use it in GitHub Desktop.
Select months by presence of entries
# select only those months for which there is at least one entry in the database table
# datetime column is DATE or DATETIME
SELECT DISTINCT(MONTH(datetime)) AS month, YEAR(datetime) AS year FROM post ORDER BY year, month
# returns table with distinct year-month pairs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment