Go Back   Forum Care Forums > Development Reference Area > MySQL Discussion

Reply
 
LinkBack Thread Tools Display Modes
How to explode or convert to nested sets?
Old
  (#1)
Guest
Guest
 
Posts: n/a
Default How to explode or convert to nested sets? - 06-04-2007, 07:49 AM

I want to expand/explode an employee table to show all the employees a
boss is responsible for (including him/herself). Then I can say SELECT
Emp FROM Employee WHERE Boss = 'Jane'. It should return Jane, Jill,
and Sally.
Or if anyone knows how to convert this to a nested set hierarchy then
that would be helpful too. I figure exploding the table would be
easier for a newbie like me. Apparently it's not that easy. Thanks...

Explode this table:

Emp Boss
John John
Tom John
Jane John
Jill Jane
Ed Tom
Sally Jill

To this table:

Emp Boss
John John
Tom John
Jane John
Jill John
Ed John
Sally John
Jane Jane
Jill Jane
Sally Jane
Tom Tom
Ed Tom
Sally Jill
Sally Sally

I have this so far. Not sure if it's the right approach. Doesn't
work. It just loops forever:

CREATE PROCEDURE expand_table()
BEGIN
DECLARE sEmp VARCHAR(10);
DECLARE sBoss VARCHAR(10);
DECLARE done INT DEFAULT 0;
DECLARE csrEmp CURSOR FOR SELECT Emp FROM Employee;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

TRUNCATE TABLE ExpEmp;
OPEN csrEmp;
REPEAT
FETCH csrEmp INTO sEmp;
IF NOT done THEN
SET sBoss = (SELECT Boss FROM Employee WHERE Emp = sEmp);
WHILE sEmp != sBoss DO
INSERT INTO ExpEmp VALUES (sEmp, sBoss);
SET sBoss = (SELECT Boss FROM Employee WHERE Emp = sBoss);
END WHILE;
END IF;
UNTIL done END REPEAT;

CLOSE csrCntr;

END$$

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: How to explode or convert to nested sets?
Old
  (#2)
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: How to explode or convert to nested sets? - 06-04-2007, 07:49 AM

EMAIL REMOVED wrote:
> I want to expand/explode an employee table to show all the employees a
> boss is responsible for (including him/herself). Then I can say SELECT
> Emp FROM Employee WHERE Boss = 'Jane'. It should return Jane, Jill,
> and Sally.
> Or if anyone knows how to convert this to a nested set hierarchy then
> that would be helpful too. I figure exploding the table would be
> easier for a newbie like me. Apparently it's not that easy. Thanks...
>
> Explode this table:
>
> Emp Boss
> John John
> Tom John
> Jane John
> Jill Jane
> Ed Tom
> Sally Jill
>
> To this table:
>
> Emp Boss
> John John
> Tom John
> Jane John
> Jill John
> Ed John
> Sally John
> Jane Jane
> Jill Jane
> Sally Jane
> Tom Tom
> Ed Tom
> Sally Jill
> Sally Sally
>
> I have this so far. Not sure if it's the right approach. Doesn't
> work. It just loops forever:
>
> CREATE PROCEDURE expand_table()
> BEGIN
> DECLARE sEmp VARCHAR(10);
> DECLARE sBoss VARCHAR(10);
> DECLARE done INT DEFAULT 0;
> DECLARE csrEmp CURSOR FOR SELECT Emp FROM Employee;
> DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
>
> TRUNCATE TABLE ExpEmp;
> OPEN csrEmp;
> REPEAT
> FETCH csrEmp INTO sEmp;
> IF NOT done THEN
> SET sBoss = (SELECT Boss FROM Employee WHERE Emp = sEmp);
> WHILE sEmp != sBoss DO
> INSERT INTO ExpEmp VALUES (sEmp, sBoss);
> SET sBoss = (SELECT Boss FROM Employee WHERE Emp = sBoss);
> END WHILE;
> END IF;
> UNTIL done END REPEAT;
>
> CLOSE csrCntr;
>
> END$$
>


Your problem is you have Jane as her own boss. This is what's causing
your loop.

If Jane is at the top, than she should have no boss (null value). If
she's not, she should have a different boss.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
EMAIL REMOVED
==================
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top