4 ms·
This is an elegant and simple formulation of the problem and shows how bad SQL is at this. However, you can do this in SQL. with recursive bacon_movies as
by seanhunter 7y ago
This is an elegant and simple formulation of the problem and shows how bad SQL is at this. However, you can do this in SQL.
with recursive bacon_movies as (
select
a.id as actor_id
, 0 as bacon_degree
from
actors a
where
a.name = 'Kevin Bacon'
union
select
r1.actor_id
, b.bacon_degree + 1 as bacon_degree
from
bacon_movies b
join roles r on r.actor_id = b.actor_id
join roles r1 on r.movie_id = r1.movie_id and r1.actor_id <> b.actor_id
where b.bacon_degree < 6
)
select
a.name
, min(b1.bacon_degree) as bacon_degree
from bacon_movies b1
join actors a on a.id = b1.actor_id
group by a.name
order by bacon_degree asc;
...not saying it's a good way to do this
- tossAfterUsing 7y agojfc... sql :( i've written thousands of lines of cypher queries. i just can't do sql though, it's not like i refuse either... it's that my brain just doesn't get it. practice would surely help. i took an intro class once, and that improved my skills a lot (i.e. 0 -> 0.1)... teaching about the math of what's being done and giving me some ways to think about it... but it all just seems so incredibly tedious.
- taffer 7y ago> ...not saying it's a good way to do this It really depends. If you do these kinds of queries all the time, it's a good idea to use a graph DB, but if these queries are rare, it often doesn't make sense to add a new technology to your project.
- bcoates 7y agoMaybe I have the brainworms from too much SQL but that seems pretty fine? It's an idiomatic CTE that more or less directly translates back to the problem statement. (nitpick: I'd exclude already-found rows from the union to do shortest-path in the CTE instead of doing it later)