2
I need to do a query, where I inform a person code and sql returns me all the people who are within that group that is the person to whom I typed the code.
Example: I enter code '1', The code refers to the person’s code, this person is in a class 'A', but in class 'A' there are more people, I need to get all the people in this class 'A'. There is the possibility of the person having more than one CLASS, then I need to take ALL PEOPLE OF ALL CLASSES OF THAT CODE I informed.
Until now manage to make a mess, but is returning 2 lines as null and brings the rest correct.
select Q5.* from
( select * FROM tbl_TURMA) Q1
left join
( select * from tbl_PESSOA_TURMA WHERE COD_IDENT_PESSO = '38' ) Q2
on Q1.COD_IDENT_TURMA = Q2.COD_IDENT_TURMA
left join
( select * from tbl_PESSOAS ) Q3
on Q2.COD_IDENT_PESSO = Q3.COD_IDENT_PESSO
left join
(select * from tbl_PESSOA_TURMA) Q4
on Q4.COD_IDENT_TURMA = Q2.COD_IDENT_TURMA
left join
(select * from tbl_PESSOAS) Q5
on Q4.COD_IDENT_PESSO = Q5.COD_IDENT_PESSO
TURMAS | ALUNOS | TURMAS_ALUNOS
1 | 1 | 1 - 1
2 | 2 | 1 - 2
3 | 3 | 2 - 1
| 4 | 2 - 5
| 5 | 1 - 6
| 6 | 3 - 6
A minha SQL com o CÓDIGO de ALUNO 1 neste caso deveria retornar:
ALUNOS 1,2,6,5
Pois o aluno 1 está na turma 1, e juntamente com ele está o aluno 2 e aluno 6.
Porem o aluno 1 também está na turma 2, e juntamente com ele o aluno 5
Don’t you want me to repeat? Then which class will appear to the person who is in two classes?
– Jean Gustavo Prates