I need to do a query, where I enter a person code and sql returns all the people who are inside that group who is the person I typed the code to.
Example: I enter code '1', The code refers to the person code, this person is in a 'A' class, but in the 'A' group there are more people, I need to get all the people in this 'A' class. There is a possibility that the person has more than one TURMA, so I need to get all people from ALL the TURKS OF THAT CODE I reported.
So far you can make a mess, but it's coming back 2 lines as null and brings the rest right.
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