Forum Discussion

Mr_Gou's avatar
Mr_Gou
Copper Contributor
Jun 29, 2022

Multiple relationships to the same table for each row

I have a database with a design similar to this:

For each record, I have two fields that are related to the same table (a directory of people). The Seller and Manufacturer fields are defined as lookups to the people table.

Now, I want to do a query that will display, for Seller and Manufacturer, the FirstName, LastName and e-mail fields of the related record for the Seller and Manufacturer.

So, if I have a product table that looks like this:

IDNameSellerManufacturer
1ProductA12

 

And a people table that looks like that:

IDFirstNameLastNameEmail
1BobSmithfake email
2KellyJonesfake email

 

I'd like to get this:

ProductSeller FirstSeller LastSeller e-mailMan FirstMan LastMan e-mail
ProductABobSmithfake emailKellyJonesfake email

 

I thought this was goind to be simple, but I simply cannot figure out how to do this!

 

Can anyone help?

 

  • Mr_Gou 

    First, this is a suboptimal approach: " The Seller and Manufacturer fields are defined as lookups to the people table." Lookups are an inferior way to manage related values in tables.  While it is true that Microsoft implemented them a long time ago, and a lot of people have been lured into using them, it's simply not a good design. Return to the basic Primary Key/Foreign Key designation and save a lot of grief for yourself.

     

    That said, the solution to creating a query here is to add the People table to the query TWICE. Alias the two instances as "Seller" and "Manufacturer" so you know which is which. Then join Primary Key of the Seller instance to the Seller Foreign Key and the Primary Key of  the Manufacturer instance to the Manufacturer Foreign Key.

  • George_Hepworth's avatar
    George_Hepworth
    Silver Contributor

    Mr_Gou 

    First, this is a suboptimal approach: " The Seller and Manufacturer fields are defined as lookups to the people table." Lookups are an inferior way to manage related values in tables.  While it is true that Microsoft implemented them a long time ago, and a lot of people have been lured into using them, it's simply not a good design. Return to the basic Primary Key/Foreign Key designation and save a lot of grief for yourself.

     

    That said, the solution to creating a query here is to add the People table to the query TWICE. Alias the two instances as "Seller" and "Manufacturer" so you know which is which. Then join Primary Key of the Seller instance to the Seller Foreign Key and the Primary Key of  the Manufacturer instance to the Manufacturer Foreign Key.

    • Mr_Gou's avatar
      Mr_Gou
      Copper Contributor
      Yes, that's it! And thanks also for the best practice advise. I really thought that lookups, although not found in other database software, was the recommended way for Access. I learned something new!
      • George_Hepworth's avatar
        George_Hepworth
        Silver Contributor

        Mr_Gou 

        Ah, "recommended". Like a lot of things, it depends on who is doing the recommending and why.:smile:

         

        For extremely basic situations, Lookup fields in tables are relatively harmless in the sense that such applications require little logic or data manipulation. However, with that simplicity comes significant limitations on flexibility and extensibility and usability. 

         

        Some long-time developers are quite adamant. Although less insistent, I personally don't use them (except in one very limited situation) and never recommend them outside that limited situation.  I'll restrain my impulse to preach further. It's worth while, though, to browse the internet for further discussions if you have time.

Resources