I would like to combine these four queries into one:
SELECT COUNT(ID) FROM conversations WHERE DAY(create_time)=DAY(NOW());
SELECT COUNT(ID) FROM conversations WHERE WEEKOFYEAR(create_time)=WEEKOFYEAR(NOW());
SELECT COUNT(ID) FROM conversations WHERE MONTH(create_time)=MONTH(NOW());
SELECT COUNT(ID) FROM conversations WHERE YEAR(create_time)=YEAR(NOW());
This single query would return to me, for example
day => # of conversations from today,
week => # of conversations from this week,
month => # of conversations from this month,
year => # of conversations from this year
I thought I was about there with some CASE syntax (I’m no MySQL expert), but realized each record seemed to only be able to be counted in one case, and I need them to overlap. In other words, I could have a single record counted for one of these cases, two, three, or all four.
Demo: http://www.sqlfiddle.com/#!2/e62f0/1