Reputation: 1638
I have 2 tables, one with poll answers and one with votes:
DESC polls;
Field Type Null Key Default Extra
id int(11) NO PRI NULL auto_increment
pollId int(11) NO MUL NULL
answer varchar(150) YES NULL
DESC votes;
Field Type Null Key Default Extra
id int(11) NO PRI NULL auto_increment
pollId int(11) NO MUL NULL
answerId int(11) NO MUL NULL
userId int(11) NO MUL NULL
I am trying to get following results with all answers and votes:
pollId answerId numberOfVotes
1 1 20
1 2 10
1 3 0
I tried right joins of votes to answers, but it does not work:
SELECT answers.id, COUNT(votes.answerId) FROM answers JOIN votes ON votes.pollId = answers.pollId GROUP BY votes.pollId;
Upvotes: 1
Views: 110