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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:19:43+00:00 2026-06-16T12:19:43+00:00

I want to be able to inner join two tables based on the value

  • 0

I want to be able to inner join two tables based on the value of a column I’m selecting.

My Query:

SELECT varA, varB, MAX(varC) varC
FROM table1
INNER JOIN table2       ON varB = table2.varB
INNER JOIN @tempTable   ON varA = table1.column
INNER JOIN table3       ON varC = table3.column AND table3.column2 = 1 -- **IF NOT varA = 100**
INNER JOIN table4       ON varC = table4.column  **-- IF varA = 100**
LEFT OUTER JOIN table5  ON table2.column = table5.column AND table5.column2 = 1 AND table5.column3 = 4
GROUP BY varB, varA

So, in short INNER JOIN on table4 if value of column(varA) is not 100 else INNER JOIN on table3

  • 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-16T12:19:45+00:00Added an answer on June 16, 2026 at 12:19 pm

    I think this will get the result you’re after:

    LEFT JOIN table3       ON varC = table3.column AND table3.column2 = 1 AND varA <> 100 -- **IF NOT varA = 100**
    LEFT JOIN table4       ON varC = table4.column  AND varA = 100   **-- IF varA = 100**
    ...
    WHERE (Table3.Column IS NOT NULL OR Table4.Column IS NOT NULL)
    

    Basically allows a join to either table, but only return rows where one of those joins found a record. If you want to return the actual columns from the table as part of the select then you could use an ISNULL:

    SELECT ISNULL(Table3.Column, Table4.Column) AS Column
    

    EDIT: I should have added that you can’t conditionally join to tables unless you want to use dynamic SQL. You can either join to the table or not. What I outlined above lets you do the join to both tables and then check that one of them did join (which is basically what an Inner Join does).

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

Sidebar

Related Questions

I want to be able to inner join two tables based on the result
In SQL, can we always write an inner join statement as a main query
I have two tables customer and activation that I would like to join. Here
I have two tables (subjects and pages) in one-to-many relations. I want to add
I want to put two tables which are containing Products together by their DESCRIPTIONs
I have two tables with a parent/child relationship. I want to update the parent
$q = SELECT u.username, p.position FROM .TBL_FOOT_TOUR_PLAYERS. p INNER JOIN .TBL_USERS. u ON p.userid
I'm using LaTeX and BibTeX for an article, and I want to able to
Want to be able to provide a search interface for a collection of objects
I want to be able to take an image that i have already captured

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.