SQL field with multiple id's of other table

前端 未结 5 485
暗喜
暗喜 2021-01-04 21:27

Could someone give me an idea how to create this database structure. Here is an example:

Table \"countries\":
id, countryname
1, \"US\"
2, \"DE\"
3, \"FR\"
4         


        
5条回答
  •  春和景丽
    2021-01-04 21:51

    You need an intersection table for that many-to-many relationship.

    Table Country
    CountryID, CountryName
    
    Table CountryProduct
    CountryID, ProductID
    
    Table Product
    ProductID, ProductName
    

    You then Inner Join all 3 tables to get your list of Countries & Products.

    Select * From Country 
    Inner Join CountryProduct On Country.CountryID = CountryProduct.CountryID 
    Inner Join Product On CountryProduct.ProductID = Product.ProductID
    

提交回复
热议问题