有人可以告诉我如何创建这个数据库结构.这是一个例子:
Table "countries":
id, countryname
1, "US"
2, "DE"
3, "FR"
4, "IT"
Run Code Online (Sandbox Code Playgroud)
现在我有另一个表"产品",在那里我想存储这个产品可用的所有国家:
Table "products":
id,productname,countries
1,"product1",(1,2,4) // available in countries US, DE, IT.
2,"product2",(2,3,4) // available in countries DE, FR, IT.
Run Code Online (Sandbox Code Playgroud)
我的问题:如何在"产品"中设计表结构以便能够存储多个国家/地区?
我最好的想法是在那里放一个以逗号分隔的字符串(即"1,2,4"),然后拆分该字符串以查找每个条目.但我怀疑这是最好的方法吗?
编辑:谢谢大家的帮助,太棒了!很难选择正确的答案,我最终选择了Gregs,因为他向我指了一个JOIN解释,并给出了一个如何使用它的例子.
Gre*_*reg 10
您需要一个多对多关系的交集表.
Table Country
CountryID, CountryName
Table CountryProduct
CountryID, ProductID
Table Product
ProductID, ProductName
Run Code Online (Sandbox Code Playgroud)
然后你加入所有3个表来获得你的国家和产品列表.
Select * From Country
Inner Join CountryProduct On Country.CountryID = CountryProduct.CountryID
Inner Join Product On CountryProduct.ProductID = Product.ProductID
Run Code Online (Sandbox Code Playgroud)