2016-02-05 127 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 

我會回來後