How do select in Mysql to get this result

Asked

Viewed 77 times

-5

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

Example of these tables

the example shows the option data it contains in the poll table, the more I would like q to show 0 if it does not contain in the poll table.

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

has several questions and 3 options to choose, I have to show as per the image below, even if ñ has vote I have to show the name.

inserir a descrição da imagem aqui

1 answer

0

First that has no unanswered question, then includes 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 the 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
  • Raoni, there’s an "edit" link there, I believe you already have that privilege.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.