|
I don't remember using recursion in a real project, but I built a HN clone on top of Postgres, with the following query: WITH RECURSIVE thread(id, parent_id, user_id, post_id, timestamp, text, depth) AS (
SELECT id, parent_id, user_id, post_id, timestamp, text, 0
FROM comments
WHERE user_id = 1
AND parent_id IS NULL
UNION ALL
SELECT c.id, c.parent_id, c.user_id, c.post_id, c.timestamp, c.text, t.depth + 1
FROM comments c
JOIN thread t ON c.parent_id = t.id
WHERE c.user_id != t.user_id
)
SELECT * FROM thread ORDER BY timestamp ASC;
|
They do come up!