SQL Beginner Queries

1

Good, someone knows what I have wrong?

I have these 3 tables:

  PREGUNTAS       PREGUNTAS_RESPUESTAS      RESPUESTAS
  idpregunta      Idpregunta_res            idrespuestas
  pregunta        idpregunta                respuesta
                  idrespuesta
                  correcto

How can I take all the questions that are there, together with their answers?

select preguntas.pregunta,respuestas.respuesta
from preguntas_respuesta
inner join preguntas on preguntas.idpregunta=preguntas_respuestas.idpregunta_res
inner join respuestas on respuestas.idrespuesta=preguntas_respuestas.idpregunta_res

I'm doing that, but it shows me only the questions and each question a response. I would have to teach the questions, and of each question 4 answers. The data is well embedded in the tables.

Thank you!

    
asked by 20.12.2017 в 12:08
source

3 answers

1

check if this helps you.

select preguntas.pregunta,respuestas.respuesta
from preguntas_respuesta
inner join preguntas on preguntas.idpregunta=preguntas_respuestas.idpregunta
inner join respuestas on respuestas.idrespuestas=preguntas_respuestas.idrespuesta
    
answered by 20.12.2017 / 12:15
source
0

Much simpler:

SELECT P.PREGUNTA, R.RESPUESTA
FROM PREGUNTAS AS P, RESPUESTAS AS R, PREGUNTAS_RESPUESTAS AS PR
WHERE PR.IDPREGUNTA = P.IDPREGUNTA 
AND PR.IDRESPUESTA = R.IDRESPUESTA
    
answered by 20.12.2017 в 12:19
0

Notice that you have the second join , the respondent as the id of the table respondents_ .

select preguntas.pregunta,respuestas.respuesta
from preguntas_respuesta
inner join preguntas on preguntas.idpregunta=preguntas_respuestas.idpregunta_res
inner join respuestas on respuestas.idrespuesta=preguntas_respuestas.idrespuesta
order by preguntas_respuestas.idpregunta

This should be part of giving you the questions with your answers sort them by the id of the question, to see them followed.

    
answered by 20.12.2017 в 12:22