Reputation: 7954
I have a database with tables Teachers,Subjects and Teachers_Subjects in my android sqlite database.My tables have structure as shown in the image.
I need to query tables to get all subjects rows that are related to a single teacher.
Initially I have the _id
of the teacher.Using teachers _id
I need to find the subjects.
According to me first I need to find all the rows in Teachers_Subjects Table related to a Teacher and then make other query using the resulted rows and Subjects Table with JOIN statement to get all rows related to that teacher.
I wanted to know is there any better way/query to accomplish this?If not then what should be the query for the solution mentioned above?
Upvotes: 2
Views: 1722
Reputation: 180300
SELECT Subjects.*
FROM Teachers_Subjects JOIN Subjects
ON Teachers_Subjects.subject_id = Subjects._id
WHERE Teachers_Subjects.teacher_id = ?
Upvotes: 1