How do I specify to mySQL’s MONTH() function to return ’08’ instead of 8 in this query?
I’d like the sort to work datewise. Currently getting results for date like
2006-9
2007-1
2007-10
2007-11
current query:
SELECT COUNT(*), CONCAT(YEAR(`datetime_added`), '-', MONTH(`datetime_added`)) as date FROM `person` WHERE (email = '' OR email IS NULL)
GROUP BY date
ORDER BY date ASC
Use the following instead:
Explanation:
DATE_FORMAT()function lets you format the date anyway you like using specifiers described in the table below (taken verbatim from documentation). So a format string'%Y-%m'means: "A full year (4 digits), followed by a dash (-), followed by a two-digit month number".Note that you can specify the language used for day/month names by setting
lc_time_namessystem variable. Extremely useful. Refer to documentation for more details.