0

I need to create a query based on two tables and a third linking table shown below.

Letter_DBID   Letter_ID  
-------        ----- 
  1             Alpha  
  2             Beta  
  3             Delta    


Fruit_DBID      Fruit_ID 
-------          -----
  1             Apples  
  2             Oranges   
  3             Bananas


Linking table:
Letter_ID      Fruit_ID
-------         -----
 Alpha          Oranges
 Delta          Bananas
 Beta           Apples
 Alpha          Bananas

The DBIDs are unique primary keys, but the IDs are non-unique. I want to create a view based on these three tables that displays how the unique DBIDs relate to eachother. Something like this:

Letter_DBID   Fruit_DBID  
-------        -----  
  1            2  
  3            3  
  2            1    
  1            3

Where the DBIDs in the same row correspond to the IDs in the linking table.

Can anyone help me figure out a way to write this query or point me in the right direction?
I am using MS Access 2010
Thanks!

2 Answers 2

0

you need to do it with two LEFT JOINS :

 SELECT Letter_DBID ,  Fruit_DBID FROM linking
 left join table2 on table2.`Fruit_ID` = linking.`Fruit_ID`
 left join table1 on table1.`Letter_ID` = linking.`Letter_ID`

DEMO HERE

1
  • Very helpful, SELECT Letters.Letter_DBID , Fruits.Fruit_DBID FROM (linking left join Fruits on Fruits.Fruit_ID = linking.Fruit_ID) left join Letters on Letters.Letter_ID = linking.Letter_ID; Ended up doing the trick!
    – Jill
    Aug 6, 2013 at 20:35
0

As the IDs aren't unique, how are you wanting to handle the overlap? For instance, say we add FRUIT_DBID: 4 FRUIT_ID: Apples Do you want both, or just one to show up in the final view? (from your example data) Letter_DBID Fruit_DBID
------- -----
2 1 2 4

If you want just one, I don't think there is a way from the non-unique data. If both are acceptable, I'd do something like

SELECT
    le.Letter_DBID, 
    f.Fruit_DBID

FROM
    (Letters le LEFT JOIN Linking li
    ON le.Letter_ID = li.Letter_ID)
    LEFT JOIN Fruits f
    ON f.Fruit_ID=li.Fruit_ID
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.