I was originally doing this through four separate queries – I think it’s time I optimize my code through the use of joins…
I have four tables (as shown below):
[sl_student_course] student_id (int 11) ------------------ course_id (int 11) ------------------ [sl_project_course] project_id(int 11) ------------------ course_id (int 11) ------------------ [sl_project] project_id (int 11) - Primary Key - _____________________ professor_id (int 11) --------------------- project_name (varchar 50) [sl_professor] professor_id(int 11) - Primary Key - _____________________ professor_name (varchar 50) --------------------- project_email (varchar 50)
What information do I need?
I need all data from sl_project and sl_professor where sl_student_course.course_id = sl_project_course.course_id AND then I need to use the project_id from sl_project_course
SO…
sl_student_course.course_id –> sl_project_course.project_id –> sl_project.professor_id, sl_project.project_name, –> sl_professor.professor_name, sl_professor.professor_name
Does this make any sense?
- If it doesn’t, here’s another example!
sl_student_course course_id | 1 sl_project_course project_id | 1 course_id | 1 sl_project project_id | 1 professor_id | 2 project_name | project1 sl_professor professor_id | 2 professor_name | John Doe professor_email | John@Doe.com
Hoping I understood your table relationships correctly, which has professors joined to projects (in the sl_project table).
This will get you project and professor data (using TSQL):