I’m attempting to model objects in MySQL using what is commonly (?) referred to as Class Table Inheritance.
See here: http://martinfowler.com/eaaCatalog/classTableInheritance.html
Essentially I have three tables:
Items (item_id,posX,posY,sort)
Items_Text (item_id,text)
Items_Image (item_id, url)
When I select data I only want to get:
item_id,posX,posY,sort,text
OR
item_id,posX,posY,sort,url
Can anyone suggest a method for doing this?
The only method I can think of is hard coding it based on an additional column “type” in the Items table or some other method of hard coding it into the application logic I’d prefer not to consider. I guess I’m hoping for some complex (or otherwise) method of using joins I’m unfamiliar with?
Thanks,
Ken
You can use left outer join, and check the results for null in the joined ID column, like this:
When you examine the result set that you get back, you will get non-null in
t.item_idonly when the item is in theItems_Text, and a non-null inu.item_idonly when the item is in theItems_Image. You can use this test to determine the type without having an explicit column.