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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:10:17+00:00 2026-05-18T09:10:17+00:00

I have three tables table1, table2 and table2. table1.parent_id is the id of a

  • 0

I have three tables table1, table2 and table2. table1.parent_id is the id of a record in table2 or table3, depending on the value of table1.parent_type. Now I want to join table1 with table2 and table3, depending on the value of table1.parent_type.

This can be done using UNION. But is there any other method?

This is my current query

 (SELECT c.*, a.title 
FROM table1 c 
LEFT OUTER JOIN table2 a 
ON c.parent_id = a.id 
WHERE c.parent_type = 0) 
UNION (SELECT c.*, p.title 
FROM table1 c 
LEFT OUTER JOIN table3 p 
ON c.parent_id = p.id 
WHERE c.parent_type = 1) 
ORDER BY id DESC 
LIMIT 10

Update : This is another method (From Dark Falcon’s reply)

SELECT c.*, IF(c.parent_type = 0, a.title, p.title) as title FROM table1 c 
LEFT OUTER JOIN table2 a ON c.parent_id = a.id AND c.parent_type = 0  
LEFT OUTER JOIN table3 p ON c.parent_id = p.id AND c.parent_type = 1 
WHERE a.id IS NOT NULL OR p.id IS NOT NULL ORDER BY id DESC LIMIT 10;

Update 2 : I profiled the queries with query profiler. The multi table join is more than 100 times faster for all my test runs.

  • 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-18T09:10:18+00:00Added an answer on May 18, 2026 at 9:10 am

    This is a little difficult without the schema, but something along these lines should work. Note thaty you can put any condition in the ON clause of the join. Why do you wish to avoid the UNION?

    SELECT c.*, a.title FROM
     table1 c
     LEFT OUTER JOIN table2 a
      ON c.parent_id = a.id AND c.parent_type = 0
     LEFT OUTER JOIN table3 p
      ON c.parent_id = p.id AND c.parent_type = 1
    WHERE a.id IS NOT NULL OR p.id IS NOT NULL
    ORDER BY id DESC LIMIT 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query which grabs the max date record for three different tables.
If I have three different tables like this table_1 Field 1: victories Field 2:
I have a database with three tables: user_table country_table city_table I want to write
I have three tables (individuals, groups & recordlabels) and want use the information between
I have two tables: table1 (id, name, connte) table2 (id, name, connte) They are
Say I have three tables: Fruit (Table 1) ------ Apple Orange Pear Banana Produce
I have three tables of data: table: cars [10,000 rows] table: planes [2,000 rows]
I have three tables. The designs were like student table create table student (studID
I have three tables. I have to retrieve the data using Linq statement. My
I have three tables: Employee(EmployeeID,Fname,Lname...) ProjectHeader(ProjectID,LeadID,Status....) ProjectDetails(ProjectDetailsID,ProjectID....) Here's my current code: $get_projects = SELECT

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.