Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with joining a table twice

Posted on 2011-03-15
4
Medium Priority
?
387 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
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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

963 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