我的数据库有7个表:一个是父(feed),另外三个是isA关系的子(公共提要,私有提要,生成的提要)。在feed表上有一个带有外键的表“subscriptions”。每个用户都可以订阅任何类型的订阅源。问题是每种类型的Feed的视图都不同,这意味着我需要为每种订阅的Feed生成不同的链接。根据当前的方案,我需要执行3个查询以从Feed id获取Feed的类型。这个问题有更好的解决方案吗?
答案 0 :(得分:0)
您可以使用视图将子项预先加入父项,并获得一致的结果,无论哪种子类型。例如:
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 ...;
或者,如果您的Feed表没有feed_type列(或等效的):
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 ...;