mysql - SQL command to show 0 if doesn't exist in the join table -
i'm having problem in sql command.
i have table questions, other possible answers questions , other replies users.
imagine following example:
question 1: 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) total replies r left join answers on a.id = r.id_answer left join questions q on q.id = a.id_question q.id = 1 group r.id_answer
my point the
select
result:
spain 10
portugal 0
but can't, don't know how it, because way did, result asnwers replies on replies table. this:
spain 10
you have start questions, , left join
replies.
select a.answer, count(r.id_answer) total questions q join answers on ( a.id_question = q.id ) left join replies r on ( r.id_answer = a.id ) q.id = 1 group a.id, a.answer
with current query don't need question
:
select a.answer, count(r.id_answer) total answers left join replies r on ( r.id_answer = a.id ) a.id_question = 1 group a.id, a.answer
see example second query on sql fiddle, returns:
answer total spain 10 portugal 0
Comments
Post a Comment