473,473 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Recursively select all Primary IDs, without stored procedure?

dlite922
1,584 Recognized Expert Top Contributor
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....
Aug 14 '08 #1
1 1895
Atli
5,058 Recognized Expert Expert
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.
Aug 16 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Patrick | last post by:
Hello I have many different tables, that most have the same structure.. they all have some primary key, some fields and mylinkid ... so what I want to make is to create a stored procedure, where...
3
by: Martin Feuersteiner | last post by:
Dear Group I wonder whether you can give me a syntax example for a SQL Statement. Lets assume I've a table containing three columns ContactID (Primary Key), Firstname and Lastname. I would...
2
by: Scott | last post by:
I have a stored procedure where I want to select all fields matching the query into another table. In addition, I want to add a common groupID to each of the records that are being inserted into...
3
by: nandan | last post by:
Hi, Has any one ever compared the performance of calling a DataTable's Select method with a stored procedure doing the same thing? My point is: dataRows = DataTable.Select(filter) is better or...
10
by: serge | last post by:
Using "SELECT * " is a bad practice even when using a VIEW instead of a table? I have some stored procedures that are identical with the difference of one statement in the WHERE clause. If I...
2
by: Beowulf | last post by:
If I run this statement in Query Analyzer, it properly returns 1 for my testing table. But if I put the statement into a stored procedure, the stored procedure returns NULL. What am I doing...
2
by: alexs | last post by:
Chaps, I'm starting to play with db2 V9.1 and am writing a stored procedure to manage accounting records from oiur RADIUS server. I've got an XML aware table with an auto increment primary...
2
by: DotNetDanjo | last post by:
I want a stored procedure to run a select query on it's own like this: Select *, user_id As UserId FROM CC_host.dbo.usr_table Where(email LIKE N'%@%usa.com') But from these results I want the...
2
by: kxyz | last post by:
Hello everyone, I need help with a stored procedure or two. My stored procedures are supposed to check if a certain record exists. If it does exist, then I select everything from that row, as...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.