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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:32:26+00:00 2026-05-28T15:32:26+00:00

How can I join 2 different tables on 2 columns where one column is

  • 0

How can I join 2 different tables on 2 columns where one column is equal and the other is not. In the example below, I need to query for records where the email matches but the NID does not.

Table A
ID              NID                  Email
1               bob                  bob@asdf.com
2               mike                 mike@asdf.com
3               dave                 dave@asdf.com

Table B
ID              NID                  Email
1               bob                  bob@asdf.com
2               mike                 mike@asdf.com
3               doug                 dave@asdf.com

Sample output should be from Table B:

ID              NID                  Email

3               doug                 dave@asdf.com

Bonus points if you can do it in LINQ to Entities

  • 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-28T15:32:27+00:00Added an answer on May 28, 2026 at 3:32 pm
    CREATE TABLE #TableA
    (
        ID int,
        NID varchar(25),
        Email varchar(50)
    )
    
    CREATE TABLE #TableB
    (
        ID int,
        NID varchar(25),
        Email varchar(50)
    )
    
    INSERT INTO #TableA(ID, NID, Email) VALUES (1, 'bob', 'bob@asdf.com')
    INSERT INTO #TableA(ID, NID, Email) VALUES (2, 'mike', 'mike@asdf.com')
    INSERT INTO #TableA(ID, NID, Email) VALUES (3, 'dave', 'dave@asdf.com')
    
    INSERT INTO #TableB(ID, NID, Email) VALUES (1, 'bob', 'bob@asdf.com')
    INSERT INTO #TableB(ID, NID, Email) VALUES (2, 'mike', 'mike@asdf.com')
    INSERT INTO #TableB(ID, NID, Email) VALUES (3, 'doug', 'dave@asdf.com')
    
    SELECT 
        #TableB.ID, 
        #TableB.NID, 
        #TableB.Email
    FROM 
        #TableB Inner Join #TableA
        ON #TableA.ID = #TableB.ID WHERE #TableA.NID <> #TableB.NID
    
    DROP TABLE #TableA
    DROP TABLE #TableB
    

    Result:

    3 doug dave@asdf.com


    In conclusion just a join with a WHERE condition

    SELECT 
        TableB.ID, 
            TableB.NID, 
            TableB.Email
    FROM 
            TableB 
    Inner Join 
            TableA
    ON TableA.ID = TableB.ID WHERE TableA.NID <> TableB.NID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 5 tables. One primary and 4 additional (they have different columns). objects
I have two tables with one identical column name, but different data. I want
I am trying to get the column values form different tables included in below
What is the difference between JOIN and UNION ? Can I have an example?
Can i join two tables using LINQ for NHibernate like I can do in
How can I join 2 lists of equal lengths (to produce a 3rd list
I have 12 columns separated by a tab. How can I join them side-by-side?
I'm looking for a query to select rows from two different tables, keeping the
I'm trying to select multiple rows over different tables but I can't get it
Another NHibernate JOIN problem. I'm trying to join two different properties from one table

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.