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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:54:26+00:00 2026-06-11T08:54:26+00:00

How can i join tables using column value? I have three tables as listed

  • 0

How can i join tables using column value?

I have three tables as listed below:

  • messages_table

    -----------------------------------------------------------------------------
    msg_id  | msg_sub   | msg_to    | to_user_type  | msg_from  | from_user_type
    -----------------------------------------------------------------------------
    001     | test      | 88        | manager       | 42        | admin
    002     | test2     | 88        | manager       | 94        | manager
    
  • admin_table

    -------------------------—
    admin_id    | admin_name
    -------------------------—
    001         | Super Admin
    
  • manager_table

    ---------------------------
    manager_id  | manager_name
    ---------------------------
    88          | Mandela
    94          | Kristen
    

How can i get the desired output as shown below with SQL query. I.e.
Join tables with respect to column values when the following criteria is met:

  1. If user_type = admin then it should join with admin_table.

  2. If user_type = manager then it should join with manager_table.

Desired output:

-----------------------------------------------------
msg_id  | msg_sub   | msg_to_name   | msg_from_name
-----------------------------------------------------
001     | test      | Mandela       | Super Admin
002     | test2     | Mandela       | Kristen

I.e. Get the join sql query based on column value.


EDIT:

I want to fetch the datafrom sql query not form the serverside coding.

I tried this query from here, i.e. Winfred’s Idea ( Answered )

However, I could not understand it.

msg_by_usertype is the column based, where the value manager then it should select manager_table and if it is admin the to admin_table

  • 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-11T08:54:27+00:00Added an answer on June 11, 2026 at 8:54 am

    As far as I understood your question, you can try this:

        SELECT msg_id,  
         msg_body,  
         usersBy.userName AS msg_by, 
         usersTo.userName AS msg_to,  
         msg_by_usertype
        FROM messages
        INNER JOIN 
        (SELECT admin_id As id, admin_name as userName
          FROM admin_table         
         UNION         
         SELECT manager_id As id, manager_name as userName
          FROM manager_table ) usersTo ON msg_to = usersTo.id  
    
         INNER JOIN 
        (SELECT admin_id As id, admin_name as userName
          FROM admin_table        
         UNION         
         SELECT manager_id As id, manager_name as userName
          FROM manager_table ) usersBy ON msg_by = usersBy.id  
    

    Here is an SQL Fiddle to see how it works. (It only works if you cant have an admin who has the same id like a manager. Id should be unique in both tables.)

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

Sidebar

Related Questions

Ok so I know we can join tables 'across' in MySQL to tables using
Can i join two tables using LINQ for NHibernate like I can do in
Can I use the USING clause to join more than 2 tables? i.e. can
I have three tables: Measurements (MeasureID, Time, Distance, Value) Events(EventID, Time Value) EventValues (EventDataID,
How I can join this queries to single select (without temp table)? SELECT t2.value,
I have three tables (these are the relevant columns): Table1 bookingid, person, role Table2
I have two tables linked by an ID column. Let's call them Table A
I have 3 tables, one which represents a supertype, with an ID column. Two
I have three MYSQL tables, simplified, with following columns: ModuleEntries (MDE_ID) ModuleFields (MDF_ID, MDF_Label)
I have three tables: 'users', 'products', and 'product_types'. The 'users' table stores all usual

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.