I have the following table:
and I have the following query:
WITH RECURSIVE users_r AS (
SELECT user_id, parent_id, 0 as level
FROM users
WHERE parent_id is null
UNION ALL
SELECT u.user_id, u.parent_id, u.level + 1
FROM users u
INNER JOIN users_r
ON (u.parent_id = users_r.user_id)
)
SELECT * FROM users_r LIMIT 1000
I want to fill "level" column with depending on the count of ancestors. But my code isn't working. It fills rows only where parent_id is null.