If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > CTE parent child in mysql

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-24-09, 05:12
FBChris FBChris is offline
Registered User
 
Join Date: May 2009
Posts: 5
CTE parent child in mysql

I would like to do a kind of a CTE query like one can do in MS-SQL.

The below data is representing a tree with an id and parent relation.

Code:
id	parent		type
1	NULL		c
2	1		o
3	2		c
4	2		o
5	1		o
6	NULL		c
7	6		c
8	7		c
9	7		o
10	NULL		c
11	10		c
12	11		o
13	10		o
14	11		o
I want to find out the closed id. It can be himself or the next up in the tree

So when I query or sp...it should look like this.

Code:
id	parent		type	lastclosed
1	NULL		c	1
2	1		o	1
3	2		c	3
4	2		o	1
5	1		o	1
6	NULL		c	6
7	6		c	7
8	7		c	8
9	7		o	7
10	NULL		c	10
11	10		c	11
12	11		o	11
13	10		o	10
14	11		o	11
The query will look through the tree from the bottom to the top, it should set the lastclosed id when it sees a 'c' in the type column.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On