我正在尝试在SQL Server中设计一个完全由查询/程序驱动的“Kevin Bacon数字查找程序”,我甚至不知道从哪里开始。如果有人可以提供一些帮助,或者指出类似的东西,我会很感激,因为我已经搜索过并且无法找到类似于我需要的东西而不依赖于使用OO编程。
“凯文培根号”指的是“六度凯文培根”,演员可以通过他或她的电影角色与演员凯文培根在六个步骤中联系起来。但是,即使超过6步,我也需要找到该过程中的步骤数。
我必须使用的架构是:
ACTOR (id, fname, lname, gender)
MOVIE (id, name, year, rank)
DIRECTORS (id, fname, lname)
CASTS (pid, mid, role)
MOVIE_DIRECTORS (did, mid)
GENRE (genre, mid)
CASTS.pid是指ACTOR.id,CASTS.mid是指MOVIE.id。
答案 0 :(得分:0)
你有没有机会扩展演员和导演的架构,为他们的凯文培根号添加一个kbnumber字段?然后,您可以轻松地创建
行的存储过程update a
set a.kbnumber = EXPR(a2.kbnumber + 1)
from top 1 actor a inner join
movie m on a.mid = m.id inner join
actor a2 on a2.mid = m.id
order by a2.kbnumber ASC
where a.kbnumber is null or a.kbnumber > a2.kbnumber + 1
update a
set a.kbnumber = EXPR(a2.kbnumber + 1)
from top 1 actor a inner join
movie m on a.mid = m.id inner join
directors a2 on a2.mid = m.id
order by a2.kbnumber ASC
where a.kbnumber is null or a.kbnumber > a2.kbnumber + 1
update a
set a.kbnumber = EXPR(a2.kbnumber + 1)
from top 1 directors a inner join
movie m on a.mid = m.id inner join
actor a2 on a2.mid = m.id
order by a2.kbnumber ASC
where a.kbnumber is null or a.kbnumber > a2.kbnumber + 1
update a
set a.kbnumber = EXPR(a2.kbnumber + 1)
from top 1 directors a inner join
movie m on a.mid = m.id inner join
directors a2 on a2.mid = m.id
order by a2.kbnumber ASC
where a.kbnumber is null or a.kbnumber > a2.kbnumber + 1
运行此过程,直到不再更新kbnumbers。任何剩余的空值都没有可追溯到Kevin Bacon的关系