Skip to content

Instantly share code, notes, and snippets.

@teburd
Last active December 14, 2015 07:48
Show Gist options
  • Save teburd/5052914 to your computer and use it in GitHub Desktop.
Save teburd/5052914 to your computer and use it in GitHub Desktop.
query
WITH RECURSIVE search_graph(id, parent, name, depth) AS (
SELECT v.id AS id, v.name AS name, e.vertex_1 AS parent, 1 AS depth
FROM vertices AS v
LEFT JOIN edges AS e ON e.vertex_0 = v.id AND e.edge_type_id = 0
UNION
SELECT v.id AS id, v.name AS name, e.vertex_1 AS parent, sg.depth + 1 AS depth
FROM vertices v, search_graph sg
LEFT JOIN edges AS e ON e.vertex_0 = v.id AND e.edge_type_id = 0
WHERE sg.parent = v.id
)
SELECT * FROM search_graph ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment