/    Sign up×
Community /Pin to ProfileBookmark

Recursive MySQL

Well on a side note this is my first post, but seems like an excellent community. Now onto business…<br>
<br>
I’ve looked around at several approaches, but basically I have a table of employees, and one column stores a supervisor ID. For example:<br><br><table class=”wysiwyg_dashes” width=””><tbody><tr class=”wysiwyg_dashes_tr”><td class=”wysiwyg_dashes_td”>ID</td><td class=”wysiwyg_dashes_td”>LastName</td><td class=”wysiwyg_dashes_td”>FirstName</td><td class=”wysiwyg_dashes_td”>Department</td><td class=”wysiwyg_dashes_td”>Manager</td><td class=”wysiwyg_dashes_td”>Title</td></tr><tr class=”wysiwyg_dashes_tr”><td class=”wysiwyg_dashes_td”>1</td><td class=”wysiwyg_dashes_td”>Smith</td><td class=”wysiwyg_dashes_td”>John</td><td class=”wysiwyg_dashes_td”>Administration</td><td class=”wysiwyg_dashes_td”><br></td><td class=”wysiwyg_dashes_td”>President</td></tr><tr class=”wysiwyg_dashes_tr”><td class=”wysiwyg_dashes_td”>2</td><td class=”wysiwyg_dashes_td”>Johnson</td><td class=”wysiwyg_dashes_td”>Sally</td><td class=”wysiwyg_dashes_td”>Administration</td><td class=”wysiwyg_dashes_td”>1</td><td class=”wysiwyg_dashes_td”>Secretary</td></tr><tr class=”wysiwyg_dashes_tr”><td class=”wysiwyg_dashes_td”>3</td><td class=”wysiwyg_dashes_td”>Waters</td><td class=”wysiwyg_dashes_td”>Andrew</td><td class=”wysiwyg_dashes_td”>HR</td><td class=”wysiwyg_dashes_td”>1</td><td class=”wysiwyg_dashes_td”>HR Manager</td></tr><tr class=”wysiwyg_dashes_tr”><td class=”wysiwyg_dashes_td”>4</td><td class=”wysiwyg_dashes_td”>McNally</td><td class=”wysiwyg_dashes_td”>Kevin</td><td class=”wysiwyg_dashes_td”>Sales</td><td class=”wysiwyg_dashes_td”>1</td><td class=”wysiwyg_dashes_td”>VP, Sales</td></tr><tr class=”wysiwyg_dashes_tr”><td class=”wysiwyg_dashes_td”>5</td><td class=”wysiwyg_dashes_td”>Watson</td><td class=”wysiwyg_dashes_td”>Adam</td><td class=”wysiwyg_dashes_td”>Sales</td><td class=”wysiwyg_dashes_td”>4</td><td class=”wysiwyg_dashes_td”>Sales Associate</td></tr><tr class=”wysiwyg_dashes_tr”><td class=”wysiwyg_dashes_td”>5</td><td class=”wysiwyg_dashes_td”>Wilkinson</td><td class=”wysiwyg_dashes_td”>Mary</td><td class=”wysiwyg_dashes_td”>Sales</td><td class=”wysiwyg_dashes_td”>4</td><td class=”wysiwyg_dashes_td”>Sales associate</td></tr></tbody></table><br>I am working on an application for mangers to login and view employee information. I could easily get it working for a manger to view employees directly beneath, but I would like managers to view ALL employees under him. For example, John Smith should see those records of people who directly report to him, but also those that report to people who directly report to him. &nbsp;In this example, John Smith should have access to ALL records. &nbsp;I am using PHP and MySQL, and just can not grasp the SQL queries behind this one. I can imagine using recursion somehow, but i’m&nbsp;fairly&nbsp;new to this.<br><br>Thank you!

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @cdanik spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.11,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...