Created
January 22, 2014 10:28
-
-
Save Arkham/8556546 to your computer and use it in GitHub Desktop.
postgres recursive queries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH RECURSIVE friendship_tree(id, name, path) AS ( | |
SELECT id, name, ARRAY[id] | |
FROM members | |
WHERE members.h1 LIKE '%Cortina%' | |
UNION ALL | |
SELECT members.id, members.name, path || members.id | |
FROM friendship_tree | |
JOIN friendships ON friendships.friend_id = friendship_tree.id | |
JOIN members ON members.id = friendships.member_id | |
WHERE NOT members.id = ANY(path) | |
) | |
SELECT * FROM friendship_tree ORDER BY path; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment