Skip to content

Instantly share code, notes, and snippets.

@lkmill
Last active February 14, 2020 11:58
Show Gist options
  • Save lkmill/c8ff132886fcc7d82e027f95fc1be619 to your computer and use it in GitHub Desktop.
Save lkmill/c8ff132886fcc7d82e027f95fc1be619 to your computer and use it in GitHub Desktop.
select
child_link_id,
child_topic_id,
parent_link_id,
parent_topic_id,
child_path,
parent_path
from (
select
lt_child.link_id as child_link_id,
lt_child.topic_id as child_topic_id,
l_child.path as child_path,
lt_parent.link_id as child_link_id,
lt_parent.topic_id as parent_topic_id,
l_parent.path as parent_path
from
links_topics as lt_child
inner join topics as t_child on t_child.id = lt_child.topic_id
inner join topics as t_parent on t_parent.id = lt_parent.topic_id
inner join links_topics as lt_parent on lt_child.link_id = lt_parent.link_id and t_parent.path @> t_child.path
) as sub;
select
lt.link_id,
lt.topic_id
from links_topics as lt
where lt.link_id in (select link_id from links_topics where links_topics.topic_id = 52)
and lt.topic_id in (
select t.id
from topics as t
inner join topics as t_b on t.path @> subpath(t_b.path, 0, nlevel(t_b.path) - 1) where t_b.id = 52
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment