get branches & childs from database - Mailing list pgsql-sql

From Marcus Krause
Subject get branches & childs from database
Date
Msg-id cpsuc5$6sl$1@news.hub.org
Whole thread Raw
Responses Re: get branches & childs from database
List pgsql-sql
Hello newsgroup!

I'm trying to build up a menu by sending ONE query to database.
Afterwards the result is used by PEAR::HTML_Menu get
a html-structered menu.

db-structure of gallery:
+----+-------+------+------+--------+-------+
| id | title | date | root | parent | level |
+----+-------+------+------+--------+-------+
| 5  | A     |  XX  |  5   |   0    |   1   |
| 1  | A2    |  XX  |  5   |   5    |   2   |
| 2  | A1    |  XX  |  5   |   5    |   2   |
| 3  | A11   |  XX  |  5   |   2    |   3   |
| 4  | A12   |  XX  |  5   |   2    |   3   |
| 6  | A21   |  XX  |  5   |   1    |   3   |
| 7  | A211  |  XX  |  5   |   6    |   4   |
| 8  | B     |  XX  |  8   |   0    |   1   |
| 9  | B1    |  XX  |  8   |   8    |   2   |
+----+-------+------+------+--------+-------+

following limitations are set:
- for root-nodes parent=0 have to be set
- result should by available after sending ONE query
- child of the specified gallery should be delivered
- branch of the specified gallery should be delivered

menu-structure after the query for id=6:

+ A
|-> A1        (optional, no need to be but nice to have)
|-> A2  |-> A21    (specified id)     |-> A211
+ B


Therefore I need the following result after sending the query:

+----+-------+--------+
| id | title | parent |
+----+-------+--------+
| 8  | B     |   0    |
| 6  | A21   |   1    |
| 7  | A211  |   6    |
| 1  | A2    |   5    |
| 5  | A     |   0    |
| 2  | A1    |   5    | (optional)
+----+-------+--------+

I currently use following query:

SELECT id,title,parent,FROM gallery
WHERE (id=root OR root IN     (SELECT root FROM gallery WHERE id=6))
AND gall_level <= (SELECT level FROM gallery WHERE id=6)+1
ORDER BY level ASC,date DESC

With this query I get the additional branch under A1, but that's not
what I want. I don't know what to do.
I'm able to use views, subselects, rules (of course) and furthermore
functions (plpgsql).

I'd be pleased, if there are any ideas

Thanks, Marcus.


pgsql-sql by date:

Previous
From: Richard Huxton
Date:
Subject: Re: can't get the order I want after inserting new rows
Next
From: Mark Collette
Date:
Subject: Updating column to link one table to another