Solved

Help with joining a table twice

Posted on 2011-03-15
4
381 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 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Latency in .net app using DB in .net 21 65
MULTIPLE DATE QUERY 15 78
Require data to appear on a single line 2 44
Trying to get a Linked Server to Oracle DB working 21 60
APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
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 shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

809 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