2016-02-05 69 views
0

我将选项值和类别存储在一个表中。我想获得一列所有可能的选项值组合。组合产品通过sql

例如:

  • 选项类别ID是(1,2,5)
  • 选项值是{(31,39,55),(61,62),(98,99) }

我想这样的列表,在一列:

31 
61 
98 

31 
61 
99 

31 
62 
98 

31 
62 
99 

39 
61 
98 

39 
61 
99 

39 
62 
98 


39 
62 
99 

55 
61 
98 

55 
61 
99 

55 
62 
98 

55 
62 
99 

请参见下面的屏幕截图

enter image description here

+0

您正在使用哪个数据库管理系统? –

回答

0

不是一个答案,只要把它在这里为你与它

架构,数据采样工作,并查询您展示。

select * into #a from(
select 1 prod,1 id union all 
select 1,2 union all 
select 1,5)a 

select * into #b from(
select 1 id,31 cat union all 
select 1,39 union all 
select 1,55 union all 
select 2,61 union all 
select 2,62 union all 
select 5,98 union all 
select 5,99 ) b 


select * from #a a inner join #b b on a.id=b.id 

这检索的你看树项目一排,为了你需要

select b1.cat 'a', b2.cat 'b', b3.cat 'c' from #b b1, #b b2, #b b3, #a a1 
where a1.id=b1.id and b2.id>a1.id and b3.id>a1.id and b3.id>b2.id 
    and b1.cat<b2.cat and b2.cat<b3.cat 
order by b1.cat, b2.cat, b3.cat 

我会回来后