Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 14th, 2008, 01:14 AM
dlite922's Avatar
Site Addict
 
Join Date: Dec 2007
Location: US
Age: 23
Posts: 658
Default Recursively select all Primary IDs, without stored procedure?

I have a company hierarchy in a table that basically depicts who is who's boss.

Employee Table:

columns are:
id: primary key , auto_increment, integer
parentID: integer, default -1
level: integer, default 0 (0 = top level, CEO or President)
name: the employee's name.

So if John was Susan's boss. their values will look like this as an example:

for John:
id: 505
parentID: -1
level: 0

Susan:
id: 679
parentID: 505
level: 1


with me so far?

Now, since there are multiple hierarchies (ie Susan may be the boss of one or more persons) How can I get a list of the persons who are under John. (This would recursively be for all people under John and every employee under each one of those.

WHERE parentID = $val can only take me to one level.


Is there away to do recursion (or something) within a select statement or do i have code a stored procedure and install it with my app?

By the way, if you have another idea of how to setup this company employee hierarchy to achieve this, let me know. This model is not set in stone.

You guys always come through, thanks!




Dan // continues googling....
Reply
  #2  
Old August 16th, 2008, 06:07 AM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Age: 22
Posts: 2,777
Default

Hi.

With a nested structure like that, I can't see a way to select them all with just one query.
You would need a separate query for each "branch" of the tree structure, based on the data retrieved by the previous query.

I would do this using whichever API you are coding in. A nice little loop or a recursive function should do the trick.
It can be done with stored procedures, but I doubt it would be pretty. It would definitely require a temporary table, or some other way to cheat the fact that MySQL doesn't have a array type.
Reply
Reply

Bookmarks

Thread Tools

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

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles