Hi! My database has 7 tables: one is the parent (feeds) and the other 3 are the children (public feeds, private feeds, generated feeds) of an isA relationship. There is a table "subscriptions" with a foreign key on the feeds table. Each user can subscribe a feed of any type. The problem is that the view for each type of feed is different, that means I need to generate different links for each type of feed subscribed. Under the current scheme I need to perform 3 queries to get the type of the feed from the feed id. Is there a better solution to this problem?
A:
You could use a view to pre-join the children to the parents, and to get a consistent result regardless of which child type. For example:
create view feed_links as
select f.feed_name
, case f.feed_type
when 'public' then pub.x + pub.y
when 'private' then pri.z
when 'generated' then gen.v + gen.w
end as link
from feeds f
left outer join public_feeds pub on pub.feed_id = f.feed_id
left outer join private_feeds pri on pri.feed_id = f.feed_id
left outer join generated_feeds gen on gen.feed_id = f.feed_id
where ...;
Or if your feeds table doesn't have a feed_type column (or equivalent):
create view feed_links as
select f.feed_name
, case when pub.feed_id is not null then pub.x + pub.y
when pri.feed_id is not null then pri.z
when gen.feed_id is not null then gen.v + gen.w
end as link
from feeds f
left outer join public_feeds pub on pub.feed_id = f.feed_id
left outer join private_feeds pri on pri.feed_id = f.feed_id
left outer join generated_feeds gen on gen.feed_id = f.feed_id
where ...;
Tony Andrews
2009-05-19 16:59:51
Hi, thanks for the answer. However I don't I got it since there is no feed_type field in the parent table.
Pedro Daniel
2009-05-19 20:13:08
See updated answer
Tony Andrews
2009-05-20 06:53:40