pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Join table with itself for heirarchial system?
2+ messages / 2 participants
[nested] [flat]

* Join table with itself for heirarchial system?
@ 2003-07-17 01:34 Benjamin Smith <bens@effortlessis.com>
  2003-07-20 07:44 ` Re: Join table with itself for heirarchial system? Oleg Bartunov <oleg@sai.msu.su>
  0 siblings, 1 reply; 2+ messages in thread

From: Benjamin Smith @ 2003-07-17 01:34 UTC (permalink / raw)
  To: pgsql-sql

Can you query a set of nested entries to simulate a heirarchial system with a 
single query? 

I'm building a nested category table with a definition like below" 

CREATE TABLE category ( 
id serial, 
parent integer not null, 
title varchar); 

Idea is that we can "nest" categories so that we have 

id 		parent		title
----------------------------------------------
1		0			Clothing
2		1			Shirts
3		1			Pants
4		1			Socks
5		4			Male
6		4			Silk 

So that, for example, id 6 would be 

Clothing -> Socks -> Silk. 

So far, I've only been able to derive this with 3 queries - 1 to get the 
parent for id #6 (Silk) another to get the parent for id #4 (Socks) and 
finally for id #1 (Clothing) and since parent ==0 I stop. 

This seems wasteful - can this be done in a single query? 



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Join table with itself for heirarchial system?
  2003-07-17 01:34 Join table with itself for heirarchial system? Benjamin Smith <bens@effortlessis.com>
@ 2003-07-20 07:44 ` Oleg Bartunov <oleg@sai.msu.su>
  0 siblings, 0 replies; 2+ messages in thread

From: Oleg Bartunov @ 2003-07-20 07:44 UTC (permalink / raw)
  To: Benjamin Smith <bens@effortlessis.com>; +Cc: pgsql-sql

You might look on our contrib/ltree module
(http://www.sai.msu.su/~megera/postgres/gist/ltree)

	Oleg
On Wed, 16 Jul 2003, Benjamin Smith wrote:

> Can you query a set of nested entries to simulate a heirarchial system with a
> single query?
>
> I'm building a nested category table with a definition like below"
>
> CREATE TABLE category (
> id serial,
> parent integer not null,
> title varchar);
>
> Idea is that we can "nest" categories so that we have
>
> id 		parent		title
> ----------------------------------------------
> 1		0			Clothing
> 2		1			Shirts
> 3		1			Pants
> 4		1			Socks
> 5		4			Male
> 6		4			Silk
>
> So that, for example, id 6 would be
>
> Clothing -> Socks -> Silk.
>
> So far, I've only been able to derive this with 3 queries - 1 to get the
> parent for id #6 (Silk) another to get the parent for id #4 (Socks) and
> finally for id #1 (Clothing) and since parent ==0 I stop.
>
> This seems wasteful - can this be done in a single query?
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
>                http://www.postgresql.org/docs/faqs/FAQ.html
>

	Regards,
		Oleg
_____________________________________________________________
Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
Sternberg Astronomical Institute, Moscow University (Russia)
Internet: oleg@sai.msu.su, http://www.sai.msu.su/~megera/
phone: +007(095)939-16-83, +007(095)939-23-83



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2003-07-20 07:44 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-07-17 01:34 Join table with itself for heirarchial system? Benjamin Smith <bens@effortlessis.com>
2003-07-20 07:44 ` Oleg Bartunov <oleg@sai.msu.su>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox