This situation involves 3 tables.
items
videos
instructions
Each Item has a column ‘entitytype’ and a handle ‘entityid’ which is the corresponding id of a row in either table videos or table instructions. Unfortunately the name of the id’s in these tables is urlid and textid respectively.
I would like to select an Item, and Join the data from either a row in videos or a row in instructions onto the selection based on what value ‘entitytype’ holds
This code is psuedo, I am new to MySQL and trying to find a means to convey my intentions
Here’s my intuition thus far:
SELECT items.*,
IF(entitytype='video', 'videos', 'instructions') as detail_table,
IF(entitytype='video', 'urlid', 'textid') as detail_id
FROM items
JOIN (detail_table) ON (items.entityid=detail_table.detail_id)
WHERE itemid=something
Just join against both tables, and select the value you need using the IF.
Your relational model is broken if a column’s reference points to another table based on another columns value.
Better: have two extra tables:
items_to_videoanditems_to_instructionswhich make a link where neededEdit: after two minutes of lying in bed and not thinking about it, I thought about it. Offcourse.