Last active
May 22, 2020 13:46
-
-
Save hackimov/8cea1582d4cad87af9f34d9ed144cdcd to your computer and use it in GitHub Desktop.
SQL RECURSIVE SEARCH CHILDS BY PARENT ID POSTGRESQL
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 catalogs_tree AS | |
( | |
SELECT | |
catalog_id, | |
1 AS hierarchy_level | |
FROM | |
catalog | |
WHERE | |
catalog_id = '".$catalog_id."' | |
AND mailbox_id = '".$session['mailbox_id']."' | |
UNION ALL | |
SELECT | |
cat.catalog_id, | |
ct.hierarchy_level + 1 | |
FROM | |
catalog cat, | |
catalogs_tree ct | |
WHERE | |
cat.parent_catalog_id = ct.catalog_id | |
) | |
SELECT | |
products.* | |
FROM | |
catalogs_tree | |
INNER JOIN products on catalogs_tree.catalog_id = products.catalog_id; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment