I have read and learned a lot here, got a lot of problems solved without asking anything so far, but I couldn’t cope with this one.
I have three tables:
-programs
->ProgramID (Primary Unique)
->ProgramName
->AuditID
-audits
->AuditID (Primary Unique)
->VenueName
->Address
-events
->EventID (Primary Unique)
->EventDate
->ProgramID
->AuditID
Events are instances of those programs that are on stage that day. Multiple instances of one program might be performed on one day, or none if it’s not played. This is basically a temporary table, repopulated daily. Tickets can be bought from programs that are played on said day.
Audits are the actual stages where the plays are performed. A theatre can have several stages.
Programs are plays for theatres that are on their repertoire for years. The last stage where it was is performed is stored for when someone retrieves the information of a program that is not played on the actual day the stage can still be linked.
I am using this query now:
SELECT
programs.ProgramID,
programs.ProgramName,
programs.AuditID,
events.EventID,
events.EventDate,
audits.VenueName,
audits.Address
FROM programs
LEFT JOIN events
ON programs.ProgramID = events.ProgramID
JOIN audits
ON programs.AuditID = audits.AuditID
WHERE programs.ProgramID = :ProgramID
- If there is no event of that program the LEFT JOIN gives me information about the program and the audit leaving the event info blank. Good.
- Also okay when the event is at the same audit where it was played last time. Great.
- The problem is if it’s played in a different audit, because the JOIN for the audits table is made based on programs.AuditID key, which is obsolete now.
What I would need is a conditional JOIN, so that when the events.AuditID is not null, the audits table is joined using events.AuditID instead of programs.AuditID.
Thanks in advance!
I believe you should be able to
JOINusingIFNULL(events.AuditID, programs.AuditID)to solve this problem…