Recursive select

Lists: pgsql-sql
From: "Martin Smetak" <minca(at)no9(dot)cz>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Recursive select
Date: 2001-05-23 09:07:56
Message-ID: 006b01c0e367$dbbe6ac0$0b01010a@no9.cz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Hi all!

Anyone know if it's possible to make a recursive select from a table ?
My problem: I got a table of "some categories" which all points to its
parrent one(tree)...shown below. And I want to select all names of
parrent categories of one child, lets say "fast[4]". Now I'm solving that
with
many SQL queries like : "SELECT main_id FROM cat WHERE id=4;"....but I would
like to optimize this.

Anyone can help or point me to a way ??

Thanks a lot,Minca

Table : CAT
ID | Main_id | Name
=========================
1 0 Car
2 1 Crash
3 1 Wash
4 3 Fast
5 1 Second_hand
6 0 House
7 3 Slow
....etc

*(root)[0]
-Car[1]
-Crash[2]
-Wash[3]
-Fast[4]
-Slow[7]
-Second hand[5]
-House[6]


From: Martín Marqués <martin(at)bugs(dot)unl(dot)edu(dot)ar>
To: "Martin Smetak" <minca(at)no9(dot)cz>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Recursive select
Date: 2001-05-28 09:08:44
Message-ID: 01052812084400.17901@bugs
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

On Mié 23 May 2001 12:07, you wrote:
> Hi all!
>
> Anyone know if it's possible to make a recursive select from a table ?
> My problem: I got a table of "some categories" which all points to its
> parrent one(tree)...shown below. And I want to select all names of
> parrent categories of one child, lets say "fast[4]". Now I'm solving that
> with
> many SQL queries like : "SELECT main_id FROM cat WHERE id=4;"....but I
> would like to optimize this.
>
> Anyone can help or point me to a way ??

Sub-selects maybe?

SELECT main_id FROM cat WHERE id=(SELECT ID FROM cat WHERE name=crash)

Could this be what yo want?

Saludos.... :-)

> Thanks a lot,Minca
>
> Table : CAT
> ID | Main_id | Name
> =========================
> 1 0 Car
> 2 1 Crash
> 3 1 Wash
> 4 3 Fast
> 5 1 Second_hand
> 6 0 House
> 7 3 Slow
> ....etc
>
> *(root)[0]
> -Car[1]
> -Crash[2]
> -Wash[3]
> -Fast[4]
> -Slow[7]
> -Second hand[5]
> -House[6]

--
Cualquiera administra un NT.
Ese es el problema, que cualquiera administre.
-----------------------------------------------------------------
Martin Marques | mmarques(at)unl(dot)edu(dot)ar
Programador, Administrador | Centro de Telematica
Universidad Nacional
del Litoral
-----------------------------------------------------------------