简体   繁体   中英

Query for correlated tables

I am currently trying to run a query that will show all tables that correlate with each other. I did not make the table design. So I am running into some trouble in making: It's not clear how office_hours table correlates with schedule table? Overall how can I display properly all tables that correlate through a query?

SELECT * 
FROM schedule
INNER JOIN semester ON schedule.semester_id = semester.id
INNER JOIN office_hours ON office_hours.id = schedule.???

I think ID from table schedule is only an auto_increment column and the proper way to join schedule from office_hours is office_hours.schedule_id = schedule.semester_id .

select      *
from        schedule 
            inner join semester 
                on schedule.semester_id = semester.id
            inner join office_hours
                on office_hours.schedule_id = schedule.semester_id

UPDATE 1

select      *
from        schedule 
            inner join semester 
                on schedule.semester_id = semester.id
            inner join office_hours
                on office_hours.schedule_id = schedule.semester_id
            INNER JOIN faculty
                ON faculty.id = office_hours.faculty_id
            INNER JOIN Section
                ON Section.faculty_ID = faculty.id AND
                    Section.Schedule_ID = Schedule.ID
            INNER JOIN class
                ON Class.ID = Section.Class_ID
            INNER JOIN major_class_br
                ON major_class_br.class_ID = Class.ID
            INNER JOIN  major_minor 
                ON major_class_br.major_minor_id = major_minor.ID

it is assumed that all ID or linking columns exists on each table that is why INNER JOIN was used. Otherwise, use LEFT JOIN .

You must use the id, which is in both tables:

...
inner join office_hours on office_hours.schedule_id = schedule.id;

Tryu this:

SELECT *
FROM 
SCHEDULE 
INNER JOIN semester 
ON schedule.semester_id = semester.id
INNER JOIN office_hours
ON office_hours.schedule_id = schedule.id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM