Bluehost.com Web Hosting $6.95

How to explode or convert to nested sets?

This is a discussion on How to explode or convert to nested sets? within the MySQL Database forums, part of the Database Forums category; I want to expand/explode an employee table to show all the employees a boss is responsible for (including him/...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-21-2007
mouac01@yahoo.com
 
Posts: n/a
Default How to explode or convert to nested sets?

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$$

Reply With Quote
  #2 (permalink)  
Old 01-21-2007
Jerry Stuckle
 
Posts: n/a
Default Re: How to explode or convert to nested sets?

mouac01@yahoo.com 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.
jstucklex@attglobal.net
==================
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:07 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0