If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Join question.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-04-05, 13:18
midibach midibach is offline
Registered User
 
Join Date: Feb 2005
Posts: 2
Join question.

Hello,

Please excuse my SQL ignorance here.

I have 2 tables that I need to query:

Tablea:

- id
- tableb_id_1
- tableb_id_2

Tableb:

- id
- name

I know that this scenario is probably better handled with 3 tables but I still like to figure it out with the current scheme.

I've used inner joins in the past to pull from both tables, but I'm not exactly sure how to get tablea.id and the tableb.name from both tableb_id_1 AND b_id_2 using 1 query.

Thanks in advance for your help.
Reply With Quote
  #2 (permalink)  
Old 02-04-05, 19:29
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
select Tablea.id
     , one.name as name1
     , two.name as name2
  from Tablea
left outer
  join Tableb as one
    on Tablea.tableb_id_1 = one.id      
left outer
  join Tableb as two
    on Tablea.tableb_id_2 = two.id
use inner joins if you are positive that both foreign keys are valid
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-05-05, 04:18
midibach midibach is offline
Registered User
 
Join Date: Feb 2005
Posts: 2
Thanks for your help. It totally skipped my mind that I could use AS in the JOIN block.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On