I have to create a mysql query to get a voting distribution of each day exceeding a particular date, something like this…
date yes_votes no_votes
------------------------------------------
2010-01-07 21 22
2010-01-07 2 0
My table is like this..
post_votes
--------------------------
id(longint)
date(timestamp)
flag(tinyint) // this stores the yes/no votes 1-yes, 2-no
I am stuck at this….
SELECT COUNT(*) AS count, DATE(date) FROM post_votes WHERE date > '2010-07-01' GROUP BY DATE(date)
this gives the total number of votes per day, but not the distribution that I want.
This is a trick that works in MySQL, as
flag=1will either beTrueorFalse. ButTrue = 1andFalse = 0in MySQL so you can add the 1s and 0s using theSUM()function.Other solutions with
IForCASEwould be better for clarity or if there is any chance you want to move the database to another RDBMS.Comments not related to the question:
dateorcountfor naming fields or tables.post_vote) and not plural – although many use plural, it gets confusing in the end. Plural is good for some fields or calulated fields, like youryes_votesandno_voteswhere we have a counting.