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

  • SEARCH
  • Home
  • 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 8213907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:16:50+00:00 2026-06-07T11:16:50+00:00

I have 4 tables like shown below Table: leave_request +————+———-+————–+————+———————-+ | request_id | staff_id

  • 0

I have 4 tables like shown below

Table: leave_request

+------------+----------+--------------+------------+----------------------+
| request_id | staff_id | applied_from | applied_to | status               |
+------------+----------+--------------+------------+----------------------+
|      1     |   10     | 01-07-2012   | 02-07-2012 | approved             |
|      2     |   12     | 02-07-2012   | 02-07-2012 | awaiting HR approval |
+------------+----------+--------------+------------+----------------------+

Table: leave_approval

+-------------+-------------+---------------+-------------+
| request_id  | approved_by | approved_from | approved_to |
+-------------+-------------+---------------+-------------+
|    1        |      1      |  01-07-2012   |  02-07-2012 |
|    1        |      2      |  01-07-2012   |  02-07-2012 |
|    2        |      1      |  02-07-2012   |  02-07-2012 | 
+-------------+-------------+---------------+-------------+

Table: staff

+-----------+-------+----------+
| staff_id  | name  | group_id |
+-----------+-------+----------+
|    1      | jack  |     1    |
|    2      | jill  |     2    |
|    10     | sam   |     3    | 
|    12     | david |     3    |
+-----------+-------+----------+

Table: group

+-----------+------------+
| group_id  | group_name |
+-----------+------------+
|    1      |    admin   |
|    2      |     HR     |
|    3      |    staff   | 
+-----------+------------+

I need to make a report by joining these tables, It should look like below:

+----------+------------+----------+-------------+-----------+--------------+-----------+
|applied_by|applied_from|applied_to|approved_from|approved_to|approved_admin|approved_hr|
+----------+------------+----------+-------------+-----------+--------------+-----------+
|   sam    | 01-07-2012 |02-07-2012|01-07-2012   |02-07-2012 | Jack         | Jill      |
|   david  | 02-07-2012 |02-07-2012|02-07-2012   |02-07-2012 | Jack         | null      |
+----------+------------+----------+-------------+-----------+--------------+-----------+

Thanks in advance 🙂

  • 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-07T11:16:51+00:00Added an answer on June 7, 2026 at 11:16 am

    Let’s take it step-by-step…

    First, the entities you’re selecting are in the leave_request table. So let’s start there:

    SELECT leave_request.* FROM leave_request
    

    Now, you need to know the data for the applied_by column in the desired results. So you join the staff table:

    SELECT
      applied_staff.name AS applied_by
    FROM
      leave_request
      INNER JOIN staff AS applied_staff ON leave_request.staff_id = applied_staff.staff_id
    

    (Note that I’m using aliases for the table names. This will come in handy later.)

    Now you need to know applied_from and applied_to, which you already have available:

    SELECT
      applied_staff.name AS applied_by,
      leave_request.applied_from,
      leave_request.applied_to
    FROM
      leave_request
      INNER JOIN staff AS applied_staff ON leave_request.staff_id = applied_staff.staff_id
    

    Now you need to know approved_from and approved_to, which are in the leave_approval table:

    SELECT
      applied_staff.name AS applied_by,
      leave_request.applied_from,
      leave_request.applied_to,
      admin_approval.approved_from,
      admin_approval.approved_to
    FROM
      leave_request
      INNER JOIN staff AS applied_staff ON leave_request.staff_id = applied_staff.staff_id
      INNER JOIN leave_approval AS admin_approval ON leave_request.request_id = admin_approval.request_id
    

    Uh oh, now we have a problem. There’s a one-to-many relationship, so now we have duplicated leave requests in the results. We need to filter that down somehow. You don’t specify how, so I’m going to make a couple assumptions: You want to know the approved_from and approved_to of the “admin” approval AND there will only be ONE “admin” approval.

    Let’s reflect those assumptions in the table joins:

    SELECT
      applied_staff.name AS applied_by,
      leave_request.applied_from,
      leave_request.applied_to,
      admin_approval.approved_from,
      admin_approval.approved_to
    FROM
      leave_request
      INNER JOIN staff AS applied_staff ON leave_request.staff_id = applied_staff.staff_id
      INNER JOIN leave_approval AS admin_approval ON leave_request.request_id = admin_approval.request_id
      INNER JOIN staff AS approved_staff ON admin_approval.approved_by = approved_staff.staff_id
      INNER JOIN group AS approved_staff_group on approved_staff.group_id = approved_staff_group.group_id
    WHERE
      approved_staff_group.group_name = 'admin'
    

    That should be better. Note that the table aliasing came in handy here because we now have two instances of the staff table for two different purposes in the same query. So we needed to distinguish them. (Keep in mind that I’m flying blind here and can’t actually test any of this. So correct me if there are any problems encountered along the way. I’m also free-handing this code because I don’t have MySQL handy, so let me know if there are syntax errors as well.)

    Now let’s add the approved_admin field to the results, which is already available:

    SELECT
      applied_staff.name AS applied_by,
      leave_request.applied_from,
      leave_request.applied_to,
      admin_approval.approved_from,
      admin_approval.approved_to,
      approved_staff.name AS approved_admin
    FROM
      leave_request
      INNER JOIN staff AS applied_staff ON leave_request.staff_id = applied_staff.staff_id
      INNER JOIN leave_approval AS admin_approval ON leave_request.request_id = admin_approval.request_id
      INNER JOIN staff AS approved_staff ON admin_approval.approved_by = approved_staff.staff_id
      INNER JOIN group AS approved_staff_group on approved_staff.group_id = approved_staff_group.group_id
    WHERE
      approved_staff_group.group_name = 'admin'
    

    Finally, we need to know the approved_hr. And null is allowed? We’re going to use a different join for this one, then. I’m also making similar assumptions to those above. Let’s try this:

    SELECT
      applied_staff.name AS applied_by,
      leave_request.applied_from,
      leave_request.applied_to,
      admin_approval.approved_from,
      admin_approval.approved_to,
      approved_staff.name AS approved_admin,
      hr_staff.name AS approved_hr
    FROM
      leave_request
      INNER JOIN staff AS applied_staff ON leave_request.staff_id = applied_staff.staff_id
      INNER JOIN leave_approval AS admin_approval ON leave_request.request_id = admin_approval.request_id
      INNER JOIN staff AS approved_staff ON admin_approval.approved_by = approved_staff.staff_id
      INNER JOIN group AS approved_staff_group on approved_staff.group_id = approved_staff_group.group_id
      LEFT OUTER JOIN leave_approval AS hr_approval ON leave_request.request_id = hr_approval.request_id
      LEFT OUTER JOIN staff AS hr_staff ON hr_approval.approved_by = hr_staff.staff_id
      LEFT OUTER JOIN group AS hr_staff_group ON hr_staff.group_id = hr_staff_group.group_id
    WHERE
      approved_staff_group.group_name = 'admin'
      AND hr_staff_group.group_name = 'HR'
    

    I’m not entirely sure about those latter LEFT OUTER JOINs. The first one is definitely going to need to be a join that allows for null values, but I’m not sure how the query engine handles joins beyond that. I’d prefer that they be INNER JOINs within the scope of the initial LEFT OUTER JOIN. But I guess all of that really also depends on the integrity of the data, which I can’t guarantee.

    It’s also worth noting that you claim to want "Jack" as output when the value is "jack". I didn’t do any string manipulation in this code to make that happen. If the value should be capitalized in the data, then capitalize it in the data.

    Again, I can’t guarantee this code. But as a walk-through it should get you moving in the right direction. As I mentioned in a comment on the question, I really recommend picking up a book on MySQL if you’re going to be writing MySQL code.

    Edit: One recommendation I can give is to the structure of the data itself. Specifically that leave_approval table feels a bit messy, and it’s that table alone which is causing the confusion. I have a couple recommendations:

    1. Add an approval_type to the leave_approval table. At the very least this would indicate if it’s an admin approval, an HR approval, or any other kind of approval. (Are there even other kinds? Will there ever be?) Then you could also use request_id and approval_type as a combined primary key, or at least a combined unique constraint, to enforce better data integrity and prevent duplicate approvals.
    2. If there are only two kinds of approvals and that’s probably not going to change, reflect them both in the leave_approval table. Have one set of columns for admin_approval_* and one set for hr_approval_*. (Each set would include the staff_id and relevant dates for the approval.) Then request_id itself could be a primary key on leave_approval making it one-to-one with leave_request. This would dramatically simplify the relational data, essentially turning a leave_approval record into an optional set of additional information for a leave_request record. The joins would become much simpler and the data would express itself much more clearly.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table similar to the example shown below. I would like to
I have two MySql tables as shown below with the data shown: CREATE TABLE
Ok I have a table in my mysql query browser like shown below: NAME:
I have two MySQL tables named 'nodes' and 'joinTable' like shown below. I need
I have a table structure like the one shown below: <table id=result> <tr> <td
I have two tables with like below codes: Table: Accounts id | username |
I would like to have a table header that looks like the image shown
I have tables like these two test_table date student test 2012-05-31 Alice Math 2012-05-31
I have two tables like, CREATE TABLE [dbo].[entry] ( [id] [int] NULL, [service] [int]
I have a table like below Consider the table id=testtable, and there is no

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.