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 8851757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:14:02+00:00 2026-06-14T13:14:02+00:00

I am working on a pretty complicated query let me try to explain it

  • 0

I am working on a pretty complicated query let me try to explain it to you. Here is the tables that I have in my MySQL database:

students Table

--- `students` ---
student_id    first_name    last_name    current_status    status_change_date
------------  ------------  -----------  ----------------  --------------------
1             John          Doe          Active            NULL
2             Jane          Doe          Retread           2012-02-01

students_have_courses Table

--- `students_have_courses` ---
students_student_id    courses_course_id    s_date      e_date      int_date
---------------------  -------------------  ----------  ----------  -----------
1                      1                    2012-01-01  2012-01-04  2012-01-05
1                      2                    2012-01-05  NULL        NULL
2                      1                    2012-01-10  2012-01-11  NULL

students_have_optional_courses Table

--- `students_have_optional_courses` ---
students_student_id    optional_courses_opcourse_id    s_date      e_date
---------------------  ------------------------------  ----------  ----------
1                      1                               2012-01-02  2012-01-03
1                      1                               2012-01-06  NULL
1                      5                               2012-01-07  NULL

Here is my query so far

SELECT 
  `students_and_courses`.student_id,
  `students_and_courses`.first_name,
  `students_and_courses`.last_name,
  `students_and_courses`.courses_course_id,
  `students_and_courses`.s_date,
  `students_and_courses`.e_date,
  `students_and_courses`.int_date,
  `students_have_optional_courses`.optional_courses_opcourse_id,
  `students_have_optional_courses`.s_date,
  `students_have_optional_courses`.e_date
FROM (
  SELECT
    `c_s_a_s`.student_id,
    `c_s_a_s`.first_name,
    `c_s_a_s`.last_name,
    `c_s_a_s`.courses_course_id,
    `c_s_a_s`.s_date,
    `c_s_a_s`.e_date,
    `c_s_a_s`.int_date
 FROM (
   SELECT
      `students`.student_id,
      `students`.first_name,
      `students`.last_name,
      `students_have_courses`.courses_course_id,
      `students_have_courses`.s_date,
      `students_have_courses`.e_date,
      `students_have_courses`.int_date
    FROM
      `students`
      LEFT OUTER JOIN `students_have_courses`
        ON (
          `students_have_courses`.`students_student_id` = `students`.`student_id` AND     (( 
          `students_have_courses`.`s_date` >= `students`.`status_change_date` AND
          `students`.current_status = 'Retread' ) OR
          `students`.current_status = 'Active')
        )
      WHERE
        `students`.current_status = 'Active' OR
        `students`.current_status = 'Retread'
  ) `c_s_a_s`
  ORDER BY
    `c_s_a_s`.`courses_course_id` DESC
) `students_and_courses`
LEFT OUTER JOIN `students_have_optional_courses`
  ON (
    `students_have_optional_courses`.students_student_id =     `students_and_courses`.student_id AND
    `students_have_optional_courses`.s_date >= `students_and_courses`.s_date AND 
    `students_have_optional_courses`.e_date IS NULL
  )
GROUP BY
 `students_and_courses`.student_id;

What I want to be returned is the student_id, first_name, and last_name for all Active or Retread students and then LEFT JOIN the highest course_id, s_date, e_date, and int_date for the those students where the s_date is since the status_change_date if status is ‘Retread’. Then LEFT JOIN the highest optional_courses_opcourse_id, s_date, and e_date from the students_have_optional_courses TABLE where the students_have_optional_courses.s_date is greater or equal to the students_have_courses.s_date and the students_have_optional_courses.e_date IS NULL

Here is what is being returned:

student_id    first_name    last_name    courses_course_id    s_date      e_date      int_date      optional_courses_opcourse_id    s_date_1    e_date_1
------------  ------------  -----------  -------------------  ----------  ----------  ------------  ------------------------------  ----------  ----------
1             John          Doe          2                    2012-01-05  NULL        NULL          1                               2012-01-06  NULL
2             Jane          Doe          NULL                 NULL        NULL        NULL          NULL                            NULL        NULL

Here is what I want being returned:

student_id    first_name    last_name    courses_course_id    s_date      e_date      int_date      optional_courses_opcourse_id    s_date_1    e_date_1
------------  ------------  -----------  -------------------  ----------  ----------  ------------  ------------------------------  ----------  ----------
1             John          Doe          2                    2012-01-05  NULL        NULL          5                               2012-01-07  NULL
2             Jane          Doe          NULL                 NULL        NULL        NULL          NULL                            NULL        NULL

Everything is working except one thing, I cannot seem to get the highest students_have_optional_courses.optional_courses_opcourse_id no matter how I form the query

Sorry, I just solved this myself after writing this all out I think it helped me think of the solution.

Here is the solution query:

    SELECT 
      `students_and_courses`.student_id,
      `students_and_courses`.first_name,
      `students_and_courses`.last_name,
      `students_and_courses`.courses_course_id,
      `students_and_courses`.s_date,
      `students_and_courses`.e_date,
      `students_and_courses`.int_date,
      `students_optional_courses`.optional_courses_opcourse_id,
      `students_optional_courses`.s_date,
      `students_optional_courses`.e_date
    FROM (
      SELECT
        `c_s_a_s`.student_id,
        `c_s_a_s`.first_name,
        `c_s_a_s`.last_name,
        `c_s_a_s`.courses_course_id,
        `c_s_a_s`.s_date,
        `c_s_a_s`.e_date,
        `c_s_a_s`.int_date
      FROM (
        SELECT
          `students`.student_id,
          `students`.first_name,
          `students`.last_name,
          `students_have_courses`.courses_course_id,
          `students_have_courses`.s_date,
          `students_have_courses`.e_date,
          `students_have_courses`.int_date
        FROM
          `students`
          LEFT OUTER JOIN `students_have_courses`
            ON (
              `students_have_courses`.`students_student_id` = `students`.`student_id` AND (( 
              `students_have_courses`.`s_date` >= `students`.`status_change_date` AND
              `students`.current_status = 'Retread' ) OR
              `students`.current_status = 'Active')
            )
          WHERE
            `students`.current_status = 'Active' OR
            `students`.current_status = 'Retread'
      ) `c_s_a_s`
      ORDER BY
        `c_s_a_s`.`courses_course_id` DESC
    ) `students_and_courses`
    LEFT OUTER JOIN (
      SELECT
        *
      FROM
        `students_have_optional_courses`
      ORDER BY
        `students_have_optional_courses`.optional_courses_opcourse_id DESC
    ) `students_optional_courses`
      ON (
        `students_optional_courses`.students_student_id = `students_and_courses`.student_id AND
        `students_optional_courses`.s_date >= `students_and_courses`.s_date AND 
        `students_optional_courses`.e_date IS NULL
      )
    GROUP BY
     `students_and_courses`.student_id;
  • 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-14T13:14:03+00:00Added an answer on June 14, 2026 at 1:14 pm

    Here is the answer that I found while debugging this myself:

        SELECT 
          `students_and_courses`.student_id,
          `students_and_courses`.first_name,
          `students_and_courses`.last_name,
          `students_and_courses`.courses_course_id,
          `students_and_courses`.s_date,
          `students_and_courses`.e_date,
          `students_and_courses`.int_date,
          `students_optional_courses`.optional_courses_opcourse_id,
          `students_optional_courses`.s_date,
          `students_optional_courses`.e_date
        FROM (
          SELECT
            `c_s_a_s`.student_id,
            `c_s_a_s`.first_name,
            `c_s_a_s`.last_name,
            `c_s_a_s`.courses_course_id,
            `c_s_a_s`.s_date,
            `c_s_a_s`.e_date,
            `c_s_a_s`.int_date
          FROM (
            SELECT
              `students`.student_id,
              `students`.first_name,
              `students`.last_name,
              `students_have_courses`.courses_course_id,
              `students_have_courses`.s_date,
              `students_have_courses`.e_date,
              `students_have_courses`.int_date
            FROM
              `students`
              LEFT OUTER JOIN `students_have_courses`
                ON (
                  `students_have_courses`.`students_student_id` = `students`.`student_id` AND (( 
                  `students_have_courses`.`s_date` >= `students`.`status_change_date` AND
                  `students`.current_status = 'Retread' ) OR
                  `students`.current_status = 'Active')
                )
              WHERE
                `students`.current_status = 'Active' OR
                `students`.current_status = 'Retread'
          ) `c_s_a_s`
          ORDER BY
            `c_s_a_s`.`courses_course_id` DESC
        ) `students_and_courses`
        LEFT OUTER JOIN (
          SELECT
            *
          FROM
            `students_have_optional_courses`
          ORDER BY
            `students_have_optional_courses`.optional_courses_opcourse_id DESC
        ) `students_optional_courses`
          ON (
            `students_optional_courses`.students_student_id = `students_and_courses`.student_id AND
            `students_optional_courses`.s_date >= `students_and_courses`.s_date AND 
            `students_optional_courses`.e_date IS NULL
          )
        GROUP BY
         `students_and_courses`.student_id;
    

    I needed to select and order the students_have_optional_courses table before LEFT JOINING it. I hope this helps any body else searching for such a answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with a pretty complicated .aspx page that is full of controls
I have a fairly complicated linq query that returns an item and a string[]
I've got an app that's working pretty flawlessly in Chrome and FF, however, when
I have a very strange situation. I´m working on a pretty big Java application
I'm working with data that is natively supplied as rational numbers. I have a
We've got a pretty complicated repository here, and I'm trying to get the most
I've been working on implementing a pretty complex system in JavaScript that needs to
I have created a pretty complicated app using some sort of factory design pattern.
I have a fairly complicated JTable subclass ( WidgetTable and its WidgetTableModel ) that
Here’s the situation: We have a 3rd party application that intermittently displays an error

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.