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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:23:20+00:00 2026-05-18T06:23:20+00:00

This is a little complicated, but I have 2 tables. Let’s say the structure

  • 0

This is a little complicated, but I have 2 tables. Let’s say the structure is something like this:

*Table1*
ID
PhoneNumber1
PhoneNumber2

*Table2*
PhoneNumber
SomeOtherField

The tables can be joined based on Table1.PhoneNumber1 -> Table2.PhoneNumber, or Table1.PhoneNumber2 -> Table2.PhoneNumber.

Now, I want to get a resultset that contains PhoneNumber1, SomeOtherField that corresponds to PhoneNumber1, PhoneNumber2, and SomeOtherField that corresponds to PhoneNumber2.

I thought of 2 ways to do this – either by joining on the table twice, or by joining once with an OR in the ON clause.

Method 1:

SELECT t1.PhoneNumber1, t1.PhoneNumber2, 
   t2.SomeOtherFieldForPhone1, t3.someOtherFieldForPhone2
FROM Table1 t1
INNER JOIN Table2 t2
   ON t2.PhoneNumber = t1.PhoneNumber1
INNER JOIN Table2 t3
   ON t3.PhoneNumber = t1.PhoneNumber2

This seems to work.

Method 2:

To somehow have a query that looks a bit like this –

SELECT ...
FROM Table1
INNER JOIN Table2 
   ON Table1.PhoneNumber1 = Table2.PhoneNumber OR
      Table1.PhoneNumber2 = Table2.PhoneNumber

I haven’t gotten this to work yet and I’m not sure if there’s a way to do it.

What’s the best way to accomplish this? Neither way seems simple or intuitive… Is there a more straightforward way to do this? How is this requirement generally implemented?

  • 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-18T06:23:20+00:00Added an answer on May 18, 2026 at 6:23 am

    First, I would try and refactor these tables to get away from using phone numbers as natural keys. I am not a fan of natural keys and this is a great example why. Natural keys, especially things like phone numbers, can change and frequently so. Updating your database when that change happens will be a HUGE, error-prone headache. *

    Method 1 as you describe it is your best bet though. It looks a bit terse due to the naming scheme and the short aliases but… aliasing is your friend when it comes to joining the same table multiple times or using subqueries etc.

    I would just clean things up a bit:

    SELECT t.PhoneNumber1, t.PhoneNumber2, 
       t1.SomeOtherFieldForPhone1, t2.someOtherFieldForPhone2
    FROM Table1 t
    JOIN Table2 t1 ON t1.PhoneNumber = t.PhoneNumber1
    JOIN Table2 t2 ON t2.PhoneNumber = t.PhoneNumber2
    

    What i did:

    • No need to specify INNER – it’s implied by the fact that you don’t specify LEFT or RIGHT
    • Don’t n-suffix your primary lookup table
    • N-Suffix the table aliases that you will use multiple times to make it obvious

    *One way DBAs avoid the headaches of updating natural keys is to not specify primary keys and foreign key constraints which further compounds the issues with poor db design. I’ve actually seen this more often than not.

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

Sidebar

Related Questions

Okay, this scenario is a little complicated, but I will try to explain it
Alright, this problem is a little complicated, so bear with me. I have a
This may be a little subjective, but I have often found that it can
I have a time (without date component) like 9am, let's say it's the current
It's a little more complicated than that, but this is my code: private void
Given this little fiddle: http://jsfiddle.net/5Sw3h/8/ I have a navigation panel to the left, a
I have this little rake task: namespace :db do namespace :test do task :reset
I have this little problem, that I cannot figure out which arguments to pass
I have this little question that's been on my mind for a while now.
I have this little problem. My client wanted two distinct swf on a web

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.