Skip to content

Instantly share code, notes, and snippets.

@hackimov
Last active May 22, 2020 13:46
Show Gist options
  • Save hackimov/8cea1582d4cad87af9f34d9ed144cdcd to your computer and use it in GitHub Desktop.
Save hackimov/8cea1582d4cad87af9f34d9ed144cdcd to your computer and use it in GitHub Desktop.
SQL RECURSIVE SEARCH CHILDS BY PARENT ID POSTGRESQL
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