Solved

Help with joining a table twice

Posted on 2011-03-15
4
384 Views
Last Modified: 2012-06-27
I have two tables where the first one relates to the second one from two fields, and I'm not sure how to set up a query properly. Tables look like:

TableA {
  main_code
  related_code
}

TableB {
  code
  name
}

Open in new window


I want to select the code and name for both the main and related code, so the output would have columns like this:

main.code | main.name | related.code | related.name

Could someone point me in the right direction?
0
Comment
Question by:jkfrench
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 15

Expert Comment

by:Aaron Shilo
ID: 35140369
select main.code ,main.name, related.code ,related.name
from main , related
where main.JOINCOLUMN = releated.JOINCOLOUMN

0
 
LVL 11

Accepted Solution

by:
David Kroll earned 500 total points
ID: 35140410
Not sure exactly which fields are supposed to match in the tables, but try this

select ta.main_code, tb1.name, ta.related_code, tb2.name
from tablea ta
inner join tableb tb1 on tb1.code = ta.main_code
inner join tableb tb2 on tb2.code = ta.related_code
0
 

Author Closing Comment

by:jkfrench
ID: 35140571
Sweet! Thank you for the quick reply, dkrollCTN.

I swapped out the table and field names for those in our database, and it does exactly what I needed.
0
 

Author Comment

by:jkfrench
ID: 35140608
Thanks for your reply, too, ashilo.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question