So I have a table that contains votes. The relevant columns for this issue being user and timestamp.
I need to grab the user's total vote count, and also their votes this month.
I know the queries - I'm not asking for those. I use these at the same time:
Votes this month / Total votes:
SELECT COUNT( 0 ) FROM votes WHERE ( timestamp BETWEEN DATE_FORMAT( NOW( ) ,'%Y-%m-01' ) AND NOW( ) ) AND user = ?;
SELECT COUNT( 0 ) FROM votes WHERE user = ?;
At the moment, my database isn't large enough (or even queried enough) to where performance is an issue. However, that's expected to change shortly. Should I keep the queries separate, or should I:
SELECT COUNT( 0 ) AS totalVotes,
SUM( IF( timestamp BETWEEN DATE_FORMAT( NOW( ) ,'%Y-%m-01' )
AND NOW( ), 1, 0 ) ) AS votesThisMonth
FROM votes WHERE user = ?;
What is the best practice? Are there any tips for querying multiple bits of information from the same table to prevent having to search it twice? Is my combined query even what I should be using?
Thanks!
count(0)instead ofcount(*)(the standard) orcount(1)(which to me makes semantic sense)?