0

I want to have a supplier table, with ID, NAME, CONTACT, PHONE, etc,

In my main table, a record can have more than one of these suppliers, what is the best way to select multiple rows from the supplier table if they relate to a record in the main table?

I was going to have a column in the main table SUPPID and have "1,3,4" as an entry so it selects record 1, 3 and 4 from the supplier table, but obviously this wont work!

any guidance is appreciated.

3
Contributors
3
Replies
16
Views
3 Years
Discussion Span
Last Post by websponge
0

You need a link table, eg.

Supplier:  ID, NAME, CONTACT, PHONE
Product: ProdID, ProdNAME
ProductSuppliers: PSID, ProdID, ID

WHere ProdID and ID in the ProductSuppliers are Foreign Keys. You can force a constraint or not.

To retrieve all the suppliers of a particular product:

SELECT Product.ProdNAME, GROUP_CONCAT(Supplier.NAME) 
    FROM ProductSuppliers AS PS 
        INNER JOIN Product 
            ON Product.ProdID = PS.ProdID 
        INNER JOIN Supplier 
            ON Supplier.ID = PS.SupplierID 
    WHERE Product.ProdID = $product_id 
        GROUP BY Product.ProdID

Off top of my head - so could be wrong.

0

Szabi, this is at planning stage, I havent created the tables yet.

Im familiar with joins, the issue is, how can i have a entry that relate to 2 or 3 records from another table, one product may have more than 1 supplier, so I need to put an entry in the product row that says "I use supplier 2,5 and 6"

Thanks

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.