How to select in a table as if it were two records

Asked

Viewed 56 times

0

On the table person has the following information

ID  |  NOME  |  É_ALUNO  |  É_RESPONSAVEL
1   |  ALUNO |  TRUE     |  FALSE
2   |  RESPON|  FALSE    |  TRUE

There is another table with the name student, in this table has the following information;

ID   |   ALUNO   |   RESPONSAVEL
1    |   1       |   2

I would like to give a select as below:

NOME   |  ALUNO
RESPON |  ALUNO

How best to give this select?

  • Se entendi direito: Seria algo como 
SELECT tmain.nome as nome_aluno, taux.nome as nome_responsavel
FROM pessoa as tmain 
INNER JOIN responsavel_aluno as tresponsavel ON tresponsavel.id = tmain.id AND tmain.É_aluno is true 
INNER JOIN pessoa as taux ON taux.id = tresponsavel.responsavel I hope I’ve helped

2 answers

3

Using INNER JOIN:

SELECT p.nome AS aluno, r.nome AS responsavel
FROM pessoa p
INNER JOIN responsavel_aluno r ON p.id = r.responsavel
WHERE p.e_responsavel = true

2


I believe this select can help:

SELECT a.nome AS aluno_nome, r.nome AS responsavel_nome
FROM pessoa a, pessoa r, responsavel_aluno ra
WHERE r.e_responsavel = true AND a.e_aluno = true AND ra.aluno = a.id AND ra.responsavel= r.id

Browser other questions tagged

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