【发布时间】:2011-01-22 00:08:04
【问题描述】:
我正在开发一个商店网站。我有一个数据库表 ProductOption,它表示适用于产品的各种选项(尺寸、颜色等):
ProductOptionId
ProductOptionGroupId
ProductId
Value
(为简洁起见,我对此进行了简化)
ProductOptionGroupId 链接到 ProductOptionGroup 表;其中一个组是 Size、Color 等,而 Value 是值 - Black、Red、Large、Small 等。
我需要为数据库中的每个产品生成所有可能的产品选项组合,受组限制。想象一下产品 1 的以下数据(其中组 1=颜色,2=尺寸,3=长度):
ProductOptionId, ProductOptionGroupId, Value
1, 1, Red
2, 1, Black
3, 1, Green
4, 2, Large
5, 2, Small
6, 3, Long
7, 3, Short
我需要生成表示以下内容的数据:
Red, Large, Long
Black, Large, Long
Green, Large, Long
Red, Small, Long
Black, Small, Long
Green, Small, Long
Red, Large, Short
Black, Large, Short
Green, Large, Short
Red, Small, Short
Black, Small, Short
Green, Small, Short
...基本上所有可能的组合,在组内。
可以有任意数量的组,每个产品可以有任意数量的产品选项。我需要能够仅通过提前知道 ProductId 来生成结果(即给我这个产品的所有产品选项组合)。
如何使用 SQL Server 2005 实现这一点?
谢谢
【问题讨论】:
-
你需要三列的结果吗?
-
不具体。我只需要某种方式来返回任何给定产品的所有产品选项组合。每个选项一行就可以了,但我需要能够将这些组合组合在一起。