クエリで case ステートメントを使用して、ロジックを一致させることができます。これは近いはずだと思います:
select m.id,
case
-- has both, use child
when p.id is not null and s.id is not null then m.company
-- has parent not second, use parent
when p.id is not null and s.id is null then p.company
-- has second not parent, use second
when p.id is null and s.id is null then p.company
-- has neither, not sure what you want to do
when p.id is null and s.id is null then 'None'
end as company
from movies as m
left join companies as s
on m.second_company_id = s.id
left join companies as p
on m.parent_company_id = p.id