Solved

Help with joining a table twice

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

759 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