1
votes

I have two tables, users and teaching

The users table contains both teachers and students. The teaching table contains the relation between the two.

I want to be able to get the student based off who "teaches" them.

Both teachers and students have an id, and I would have the teachers id to look up in the teaching table to find the students they teach.

Any ideas as to how this is done? I would like it to return the id of the student.

I've tried doing a simple join statement, but this would fetch the teacher, not the student.

Thanks in advance

2
Possible duplicate of mySQL join same table twice - RisingSun
Sample data and desired results would really help. - Gordon Linoff

2 Answers

0
votes

Something like that?

SELECT users.id FROM users INNER JOIN teaching on users.id = teaching.userId WHERE teaching.teacherId = '';
0
votes

You just need to join the users table again to the teaching table. Just make sure in one join you are using the teachers' ids and in the other the students' ids.

You can join the same table multiple times and that is totally legal in MySQL