Postgres recursive query to update values of a field while traversing parent_id
This is the table user_id | parent_id | lft --------|-----------|----- 1 | | 0 2 | 1 | 0 3 | 1 | 0 4 | 2 | 0 Here is a query to do a CTE from node 1 and traverse all the children of user_id 1 until a leaf is reached and update the value of the travesed chidren lft field to 1 WITH RECURSIVE d AS ( SELECT user_id FROM btrees WHERE user_id = 1 UNION ALL SELECT c.user_id FROM d JOIN btrees c ON c.parent_id = d.user_id ) UPDATE btrees b set lft = 1 FROM d WHERE d.user_id = b.user_id I am just asking for a query that will go in the opposite direction .. ie. from any node to the root node so I can