In my DB, I have 4 tables: Aluno
, Ano
, Matérias
and Nota
. They are mounted as follows:
Tabela :: Colunas
----------------------------------------------------------
Aluno :: AlunoID | AlunoNome | AnoID
Ano :: AnoID | AnoNome
Materia :: MateriaID | MateriaNome | AnoID
Nota :: NotaID | AlunoID | MateriaID | Nota
I'm trying to use a inner join
to merge all data:
Aluno a INNER JOIN Ano b on ( a.AnoID = b.AnoID )
INNER JOIN Materia c on ( b.AnoID = c.AnoID )
INNER JOIN Nota d on ( a.AlunoID = d.AlunoID )
In fact, I'm getting the data together! I'm just having a problem -
in the Aluno
table, I have two records. When I use echo
to display students, names are repeated for each subject and each grade.
It looks something like this:
Aluno1 Ano1 Materia1 Nota1
Aluno1 Ano1 Materia2 Nota1
Aluno1 Ano1 Materia1 Nota2
Aluno1 Ano1 Materia2 Nota2
Aluno2 Ano2 Materia3 Nota3
Aluno2 Ano2 Materia4 Nota4
Aluno2 Ano2 Materia3 Nota3
Aluno2 Ano2 Materia4 Nota4
As can be seen in the example above, the query result repeats the name and subject, and toggles the display of the rest of the results!
I would like the result to look something like this:
Aluno1 Ano1 Materia1 => Nota1 / Materia2 => Nota2
Aluno2 Ano2 Materia3 => Nota3 / Materia4 => Nota4
As you can see, the name now repeats itself only once, and the other information about that name is placed next to it!
I know that I can solve this problem by increasing the number of columns directly in the aluno
table, putting several columns called notai
, type nota1
, nota2
, nota3
, but in this way I will stay very limited.
My question then is:
How to merge the tables so that names are not repeated and information about the student appears next?
I would like to establish the real relationship, since a student belongs to a specific year, that specific year has X amount of subjects and each student of each year has earned Y grade in X subject.
I can not do this, it's getting all jammed up like in the examples above.