Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8017707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:55:21+00:00 2026-06-04T20:55:21+00:00

I have three tables that I use which has proved this problem to be

  • 0

I have three tables that I use which has proved this problem to be way out of my league.

The first is the hierachy table which looks like this

USER_ID, USER_LEVEL, Store_Manager_ID, Team_Manager_ID
------------------------------------------------------
1, 0, -, -
2, 1, 1, -
3, 2, 1, 2
4, 2, 1, 2
5, 2, 1, 2

This just shows the hierachy of the user. So user 1 has a level of 0 which means he is th store manager and so he has no store manager on top of him so his store manager id is nothing and team manager is obviously nothing.
The second user is the team manager and so level = 1 and store manager is 1(the first guy)
The third, forth and fifth are workers under 1 and 2 respectively.

The second is a login table which looks like this

USER_ID, EMAIL
--------------------------
1, john.smith@gmail.com
2, ron.jones@gmail.com
3, tom.graham@gmail.com
4, bill.small@gmail.com

The email is the login, no password.

The third is an alias table which looks like this

USER_ID, ALIAS_ID
--------------------
2, 5

The alias tables shows how user 5 is an alias of user 2. So he is the same person and has the same login. However he can be in two places in the hierachy at once i.e. a team manager and a worker at the same time.

My question is.
If I know that user 2 will be logging in as his alias, i.e. a worker before login.
When user 2 logs in, how can I select the id of the leader of the users account based on the level.

For example if I logged in as ron.jones@gmail.com (user 2) under my alias account.
The alias id of user 2 is 5 and based on the hierachy user 5 is level 2 i.e. a worker and so I should return the id of his team manager not his store manager.
So I would then return 2, i.e. himself (a little bizarre in the this case but besides the point)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:55:22+00:00Added an answer on June 4, 2026 at 8:55 pm

    This will give the answer you are looking for (if I understand the question correctly):

    SELECT team_manager_id
    FROM   hierarchy h
    INNER JOIN aliases a ON h.user_id = a.alias_id 
    INNER JOIN login l ON a.user_id = l.user_id
    WHERE  email = 'ron.jones@gmail.com';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say that I have three tables, customers, orders and orderDetails. I'm doing this:
Here's my problem: I have three tables that represent a many to many relationship
I have a MySQL query that: gets data from three tables linked by unique
I have a Microsoft SQL Server 2008 query that returns data from three tables
I have a table that has three different date columns, so I set each
I have a table in my data base that has three columns: Screen, Icon,
I have a legacy mysql database and there's this table which has a few
I have three views that I've manually created in the DB. First view is
I have two database tables: 'MyTable' which has a standard auto-incrementing primary key integer
I have a number of tables that use the trigger/sequence column to simulate auto_increment

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.