I’m having a problem in a SQL command.
I have a table with questions, other with the possible answers to that questions and other with the replies from the users.
Imagine the following example:
Question 1: Who will win semi-final?
Aswners: A) Portugal B) Spain
Replies: 10 people voted B) Spain, 0 people voted A) Portugal
SELECT a.answer, COUNT(r.id) as total
FROM replies r
LEFT JOIN answers a ON a.id = r.id_answer
LEFT JOIN questions q ON q.id = a.id_question
WHERE q.id = 1
GROUP BY r.id_answer
My point is to get from the
SELECT the result:
Spain 10
Portugal 0
But i can’t, i don’t know how to do it, because the way i did, i always get only the result from the asnwers with replies on the replies table. Like this:
Spain 10
You would have to start with your questions, and
LEFT JOINthe replies.With your current query you don’t even need the
question:See this example for your second query on SQL Fiddle, that returns: