0

I need some advice of how to setup my tables I currently have a product table and a product codes table.

In the codes table I have an id and a title such as:

1 567902
2 345789
3 345678

there can be many items in this table.

In my product table I have the usual product id,title, etc but also a code id column that I'm currently storing a comma separate list of ids for any codes the product needs to reference.

in that column I could end up with ids like: 2,5,6,9

I'm going to need to be able to search the products table looking for code ids for a specific set this is where I've come into problems trying to use id IN ($var) or FIND_IN_SET is proving problematic I've been advised to restructure it I'm happy to do just wondering what the best method would be.

4

1 に答える 1

0

2つの選択肢があるようですね。これが1対多の関係である場合は、codeテーブルではなく、テーブルに外部キーを含める必要がありproductます。

すなわち

codeId code   productId
1      567902 2
2      345789 6
3      345678 9
4      345690 9

もう1つのオプションは、productIdとcodeId(両方とも外部キーとして)を含む別のテーブルを作成することです。これは多対多の関係です。これは、コードを複数の製品に割り当てることができる場合に行うべきことです(私はそうではないと思います)。次のようになります。

codeId productId
1      2
1      10
2      6
3      9
4      9

最初の選択肢はあなたが必要としているものだと思います。

于 2012-06-26T23:17:35.597 に答える