I’m trying to conditionally join one master event table to three others depending on an event type. The select statement works fine, and returns the result set I’d expect, but when I add the JOIN statements, I get an error saying the column aliases were not found:
SELECT
event.type as type,
IF(type = 'birthday', event.target_id, NULL) as birthday_id,
IF(type = 'graduation', event.target_id, NULL) as graduation_id,
IF(type = 'wedding', event.target_id, NULL) as wedding_id
FROM event
LEFT OUTER JOIN birthday ON birthday_id = birthday.id
LEFT OUTER JOIN graduation ON graduation_id = graduation.id
LEFT OUTER JOIN wedding ON wedding_id = wedding.id
Gets this error:
Unknown column ‘birthday_id’ in ‘on clause’
UPDATE: Ok Sebas just indicated you can’t join on calculation results, in which case my approach is off. So what is the correct approach for doing something like this?
should do the trick, give me feedback!
rgds.
edit: See the IS NULL conditions. I didn’t test it, I wonder if mysql would accept it! If yes, then almost only the necessary joins would be done…