Mysql subquery sum of votes
I've got a table of votes, alongside a table of 'ideas' (entries)
Basically, I want to retrieve an object that can be read as:
id,
name,
title,
votes : {
up : x,
down : y
}
So it's simple enough until I get to the subquery and need to do two sets
of SUMs on the positive and negative values of votes.
SELECT id,name,title FROM ideas
LEFT JOIN votes ON ideas.id = votes.idea_id
My (simplified) votes table looks something like:
id
idea_id
value
Where value can be either a positive or negative int.
How would I go about getting the above object in a single query?
Bonus point: How would I also get an aggregate field, where positive and
negative votes are added together?
No comments:
Post a Comment