Two tables using Where

Asked

Viewed 25 times

-1

I have the ID of a user who is in $user.

I have two tables.

Table A (contains questions)

 coluna idA
 coluna perguntaA

Table B (stores the id of a question answered by a user)

 coluna idB
 coluna idusuarioB

1° User enters a page X showing a random question from Table A.

2° When he answers, his and the question’s data are stored in Table B (idusuario = $usuario and idB = id of the question he answered).

3° He will no longer see that particular question when he enters page X. That is, I want my select of page X not show a question already answered by the user.

1 answer

0


Use the clause no exists or not in:

select *
from  perguntas a
where 1 = 1 
  and not exists( select *
                   from perguntas_respondidas b
                   whre  1 = 1 
                     and b.id_pergunta  = a.id_pergunta
                     and b.id_aluno  = 12);
  • Carlos, thank you very much, it worked perfectly, a hug my friend!

  • Luiz, I’m glad I contributed. Hug!

Browser other questions tagged

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