I have a table (lets call it main_guys) that looks like:
id, other_item_id, other_item_type, another_column, group_id
The other_item_type
tells me which additional table holds additional information identified by the other_item_id
.
Additional columns required in the query depends on the table identified by other_item_type
. So for other_item_type == 'Type-X'
I need to get values from columns foo
and bar
from the X
table. While for other_item_type == Type-Y
I need to get values from columns ick and blah
from Y table
.
In a perfect world, I would be able to get something like:
id, other_item_id, other_item_type, another_column, group_id, foo, bar, ick, blah
- with values filled in where needed per type and other columns being null or what not if not needed.
One additional issue is that other_item_type
and other_item_id
can also be empty - and the rest of the columns in that row need to be returned in the select - but in the empty case none of the additional columns across the other tables should contain any values.
In terms of pseudo-code...
// obviously not proper code - just trying to communicate need
// Note: Need all items from main_guys table -
// even if main_guys.other_item_type is empty
select * from main_guys where main_guys.group_id == "12345"
if main_guys.other_item_type == "Type-X"
select x.foo, x.bar from x where x.id == main_guys.other_item_id
else if main_guys.other_item_type == "Type-Y"
select y.ick, y.bar from y where y.id == main_guys.other_item_id
Thanks for any help or suggestions.