I’ve been having trouble finding a way to retrieve data – I need a way to traverse multiple foreign key relationships in a mysql query. The general table structure is as follows:
Subjects (table)
-id
-fname
-lname
-etc.
Events(table):
-id
-subject_id
-procedure_id
-date
Procedures(table):
-id
-description
These identifiers are used in other tables, such as:
Assessment1(table):
-id
-event_id
-data, etc.
Assessment2(table):
-id
-event_id
-data, etc.
The problem I’m having is in being able to pull all of the assessment data for subjects that have events for both Assessment 1 and Assessment 2. I’ve been able to successfully pull data for one assessment, or for two assessments that share the same event_id, in this example, Assessment1 and Assessment 1a. But I need a way to be able to link based on the subject_id, which is not in the assessment table as this goes through the Events table.
The query that I have used successfully (based off of event_id) is:
select
s.first_name,
s.last_name,
s.ssn,
e.subject_id,
n.*,
l.Q23
from
subjects s,
events e,
Assessment1 n,
Assessment1a l
where
e.subject_id = s.id
and l.event_id = e.id
and l.Q23 = "1"
and n.event_id = e.id
How can I modify this to pull data for assessments with different event_ids but the same subject_ids?
I think this is what you need.