How do I select in Mysql to get this result

-5

I have 3 tables, I would like the return of the select to come with a value of 0 when it is not in the related table.

Example tables

The example shows the option data that it contains in the poll table, but would like it to show 0 if it does not contain the table in the poll.

These tables refer to a satisfaction survey, I would like to get the result of the select to insert into the Chart.

You have several questions and 3 options to choose, I have to show according to the image below, even if ñ I have a vote I have to show the name.

    
asked by anonymous 20.10.2017 / 20:24

1 answer

0

First that you have no unanswered questions, then include one for the test:

INSERT INTO 'pergunta' ('id_pergunta', 'titulo', 'status') VALUES
  ('5', 'O que achou da sua Pergunta ?', '1'),
  ('6', 'O que achou do Ambiente ?', '1'),
  ('7', 'O que achou do Atendimento ?', '1'),
  ('8', 'O que achou do tempo de Espera ?', '1');

Then I hit select:

SELECT p.id_pergunta AS 'Pergunta', o.titulo AS 'Opcao', COUNT(o.titulo) AS 'Total'
FROM enquete e
RIGHT JOIN pergunta p ON p.id_pergunta = e.id_pergunta
LEFT JOIN opcao o ON o.id_opcao = e.id_opcao
GROUP BY e.id_pergunta, o.titulo
    
20.10.2017 / 20:34