Solved

Help with joining a table twice

Posted on 2011-03-15
4
379 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
  • 2
4 Comments
 
LVL 15

Expert Comment

by:Aaron Shilo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for your reply, too, ashilo.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now