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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:39:52+00:00 2026-05-25T23:39:52+00:00

I have multiple MySQL tables containing varying numbers of columns. After joining three of

  • 0

I have multiple MySQL tables containing varying numbers of columns. After joining three of the tables, I have a resulting table that’s structured as follows:

+------------+------------+-----------+-------+------+
| student_id | first_name | last_name | class | rank |
+------------+------------+-----------+-------+------+
| 1          | John       | Doe       | 2012  | 1    |
+------------+------------+-----------+-------+------+
| 2          | Suzy       | Public    | 2013  | 12   |
+------------+------------+-----------+-------+------+
| 3          | Mike       | Smith     | 2014  | 50   |
+------------+------------+-----------+-------+------+

I also have two additional tables that aren’t involved in the initial join:

interest

+-------------+------------+-----------------------+----------------+
| interest_id | student_id | employer_interest     | interest_level |
+-------------+------------+-----------------------+----------------+
| 1           | 1          | Wayne Enterprises     | High           |
+-------------+------------+-----------------------+----------------+
| 2           | 1          | Gotham National Bank  | Medium         |
+-------------+------------+-----------------------+----------------+
| 3           | 2          | Wayne Enterprises     | Low            |
+-------------+------------+-----------------------+----------------+
| 4           | 3          | Gotham National Bank  | High           |
+-------------+------------+-----------------------+----------------+

offers

+----------+------------+-----------------------+
| offer_id | student_id | employer_offer        |
+----------+------------+-----------------------+
| 1        | 1          | Wayne Enterprises     |
+----------+------------+-----------------------+
| 2        | 1          | Gotham National Bank  |
+----------+------------+-----------------------+
| 3        | 2          | Wayne Enterprises     |
+----------+------------+-----------------------+

The interest and offers table won’t necessarily contain a record for every student_id but at the same time contain multiple records that reference a single student_id.

For each of the latter two tables, I’d like to:

  1. Select all rows where the employer_interest or employer_offer value is equal to $var (a variable I’ve set in PHP)
  2. Join these rows to the original table

For example, if $var is set to Wayne Enterprises, I’d like the resulting table to be:

+------------+------------+-----------+-------+------+-------------------+----------------+-------------------+
| student_id | first_name | last_name | class | rank | employer_interest | interest_level | employer_offer    |
+------------+------------+-----------+-------+------+-------------------+----------------+-------------------+
| 1          | John       | Doe       | 2012  | 1    | Wayne Enterprises | High           | Wayne Enterprises |
+------------+------------+-----------+-------+------+-------------------+----------------+-------------------+
| 2          | Suzy       | Public    | 2013  | 12   | Wayne Enterprises | Low            | Wayne Enterprises |
+------------+------------+-----------+-------+------+-------------------+----------------+-------------------+
| 3          | Mike       | Smith     | 2014  | 50   | NULL              | NULL           | NULL              |
+------------+------------+-----------+-------+------+-------------------+----------------+-------------------+

Is what I’m trying to do possible using just a MySQL query? If so, how do I do it?

  • 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-05-25T23:39:53+00:00Added an answer on May 25, 2026 at 11:39 pm

    it sounds like you just need a LEFT JOIN to the other tables since it appears you want to see all students from the first set regardless of any job offer/interest.

    If so… ensure both the “Interest” and “Offers” tables have an index where the student ID is either a single element index, or first in that of a compound index.

    select STRAIGHT_JOIN
          ORS.Student_ID,
          ORS.First_Name,
          ORS.Last_Name,
          ORS.Class,
          ORS.Rank,
          JI.Employer_Interest,
          JI.Interest,
          OFR.Employer_Offer
       from 
          OriginalResultSet ORS
    
             LEFT JOIN Interest JI
                ON ORS.Student_ID = JI.Student_ID
               AND JI.Employer_Interest = YourPHPVariable
    
                LEFT JOIN Offers OFR
                   on JI.Student_ID = OFR.Student_ID
                  AND JI.Employer_Interest = OFR.Employer_Offer
    

    To prevent “NULL” results in the employer interest, interest and offer, you can wrap them in a Coalesce() call such as (for all three columns on left join)

    COALESCE( JI.Employer_Interest, " " ) Employer_Interest
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple columns in a mySQL table. Three of the columns are named
My MysQL database contains multiple MyISAM tables, with each table containing millions of rows.
I have three tables, table one (tableA) containing users data like name and email,
I have a mysql table which holds lots of data across multiple tables. So
I have a many-to-many table relationship in MySQL involving three tables: tickets , ticket_solutions
I have MySQL tables looking like that regions table id | region ------------------- 1
I have multiple (13 in numbers) tables that have one thing in common: Their
I have two tables that can have multiple records linking to another table, but
I have multiple tables in a MySQL database. Lets say they look like this:
I have multiple tables in a mysql database and I am trying to put

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.