I'm using mysql.
I know how to do this with multiple queries but I'm trying to get it to all work in one query. I have three tables, described below.
registrations
registration_id
student_id
event_id
status
team_rosters (multiple students per team)
team_roster_id
team_id
student_id
teams (multiple teams per event)
team_id
event_id
team_name
A student that is registered may or may not be on a team. I'm trying to get the following:
Registration data for a student (based on student_id). For each event the student is registered, return the team name if he/she is on a team, if any. If not on a team, return NULL for team name.
I've got a query that comes close but no cigar.
SELECT regs1., team_rosters., teams.*
FROM registrations AS regs1 LEFT JOIN team_rosters USING (student_id),
registrations AS regs2 LEFT JOIN teams ON regs2.event_id=teams.event_id
WHERE regs1.student_id=20 AND regs2.student_id=20 AND team_rosters.team_id=teams.team_id
THANKS in advance!