Recursive queries

From: Juan Jose Comellas <juanjo(at)comellas(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: Recursive queries
Date: 2001-09-21 16:14:36
Message-ID: 200109211614.f8LGEZk21143@mens.hq.novamens.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have a database with the following fields:

product_id INTEGER
parent_id INTEGER
name VARCHAR(64)

The idea is that it will store a tree of products and its subproducts. Any
row whose parent_id is NULL will be assumed to be a root product (i.e. top
level).

This means I will need to process recursively, with some sort of reference to
parent_id passing through layers of recursion.

However, I want to save on database queries and do a "SELECT *", then fetch
each row on the results recursively, to build the tree. The idea is to only
have to do one db query.

Does anyone have any idea how this can be done in PostgreSQL? Both Oracle and
DB2 support this with their own proprietary syntax.


--
Juan Jose Comellas
(juanjo(at)comellas(dot)org)

Responses

Browse pgsql-general by date

  From Date Subject
Next Message R Talbot 2001-09-21 16:42:49 Re: Problem: Failed Make on Linux Pgres v. 7.1.2
Previous Message Tom Lane 2001-09-21 14:36:38 Re: psql and security