Created
August 13, 2018 12:35
-
-
Save alexesDev/9ac33a7538a346af5005c65ae8401b1a to your computer and use it in GitHub Desktop.
PostgreSQL: search a top-level root parent id
This file contains hidden or 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 r as ( | |
select id, parent_id | |
from products | |
where id = 100 | |
union | |
select p.id, p.parent_id | |
from products p | |
join r on p.id = r.parent_id | |
) | |
select id from r where parent_id is null; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment