Your mistake lies in this passage:
FROM aluno, professor as A
INNER JOIN aluno_has_professor as B ON (B.professor_idprofessor = A.idprofessor)
Ended up mixing two ways to make select, using INNER JOIN or not.
You perform select in two ways:
SELECT * FROM professor AS A
INNER JOIN aluno_has_professor as B ON
(B.professor_idprofessor = A.idprofessor)
Or
SELECT * FROM professor AS A, aluno_has_professor as B
WHERE B.professor_idprofessor = A.idprofessor
Whenever using more than one table you should make the relationship between them and the difference in this two select’s is that using the INNER JOIN the relationship is done after the ON and NOT USING the INNER JOIN the relationship is done after the WHERE.
In your code you ended up mixing the two ways to assemble your select and put FROM student, teacher and do not put any relationship between the two tables, you end up bringing everything from the two tables. That’s why you bring the student W even if it’s not the teacher with id = 2.
You have to interpret as follows, you have a student table, a teacher table and a link table between the two, your select should be made from the student table and the teacher table for the link table.
Select * from professor as A
INNER JOIN aluno_has_professor as B ON (B.professor_idprofessor = A.idprofessor)
INNER JOIN aluno as C ON (B.aluno_idaluno = C.idaluno)
WHERE idprofessor = 2;
I don’t know if the student table fields are correct, but the logic is this connecting your two tables in the link table.
With this when choosing the ID = 2 teacher, the link table will identify only students who are the ID = 2 teacher in the student table.
A student can only have one teacher or can several?
– Woss
Your sql is selecting all student, you have to add a Join between student and student
– edson alves