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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:38:16+00:00 2026-05-31T13:38:16+00:00

To make it easy, there are table1 (id, field1, other_table_id), table2 (id, field1) and

  • 0

To make it easy, there are table1 (id, field1, other_table_id), table2 (id, field1) and table3 (id, field1). I have a SELECT on table1 but depending if the value of field1 = 2 or 3, it should JOIN with table 2 or 3… (the other_table_id is then either the id of table2 or table3).

Can I use CASE for that? If not, how can I do it best? Can I do it in one query?

Thank you very much in advance!


PS: I think this is a similar topic: MySQL query where JOIN depends on CASE but I did not understand the solution. Also, I don’t get how to use CASE from MySQL (http://dev.mysql.com/doc/refman/5.0/en/case-statement.html)…


EDIT: Thank you for your answer, ypercube! I am sorry, I described the structure not completely right. Actually, the tables look like this:

table1: id, assigned_to, assigned_id
rooms_assigned: id, objects_id, room_type_id
objects: id, …

So, I have the object id in PHP $object_id, so how do you use this in the WHERE in the query?

According to your answer, would the right query look like this then?:

SELECT COALESCE (ra.objects_id, o.id) AS objects_id
FROM table1 as t
LEFT JOIN rooms_assigned AS ra
ON (ra.id,2)=(t.assigned_id,t.assigned_to)
LEFT JOIN objects AS o ON (o.id,3)=(t.assigned_id,t.assigned_to)

WHERE ?

EDIT 2: I think I solved the question about the “WHERE” for myself. I simply did not use WHERE in the query statement, but added it to both “ONs”, like ON (ra.id,ra.object_id,2) = (t.assigned_id,”.$object_id.”,t.assigned_to)…

Thank you again, you were a great help! 🙂

  • 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-31T13:38:17+00:00Added an answer on May 31, 2026 at 1:38 pm

    Use two LEFT joins and COALESCE() in the SELECT list:

    SELECT
        COALESCE(t2.someColumn, t3.someColumn) AS someColumn
    
    FROM
          table1 AS t
      LEFT JOIN
          table2  AS t2
              ON (t2.id, 2) = (t.other_table_id, t.field1)
      LEFT JOIN
          table3  AS t3
              ON (t3.id, 3) = (t.other_table_id, t.field1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's make this easy. I have a table. visitID studentname dob roll.no value displIndex
Hey, I'm just wondering if there's any easy function to make text suitable to
I have a main domain MYDOMAIN.COM I want to make it easy for me
I have a JList with a key listener to make it easy for the
I know there's an easy way of doing this, but my recursion abilities are
Table1 has data to be imported into Table2. Table1 contains only varchar fields, but
I have a circle image that's fairly large in size to make it easy
I want to make an easy to deploy Windows application and was was wondering
I'm looking for a way to make it easy for technically unsophisticated users to
I am looking for something that will make it easy to run (correctly coded)

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.