![](/img/trans.png)
[英]SQL pivot - How to get all possible combinations between columns to be pivoted
[英]How to merge 2 columns with all possible combinations in SQL?
這個問題聽起來令人困惑,但看看:
這樣我們就可以獲得第一列( col1 ):
select distinct maker
from product
第二列( col2 ):
select distinct type,maker
from product
所以現在我需要從col1和col2獲得所有可能的組合。 有什么建議嗎?
不久,這個:
A f1
B f2
應該成為這樣的:
A f1
A f2
B f1
B f2
PS此查詢不會返回我需要的內容。
select distinct A.maker, B.type
from product as A
使用cross join
獲取所有組合:
select m.maker, t.type
from (select distinct maker from product) m cross join
(select distinct type from product) t;
這是ANSI SQL語法,應該在任何數據庫中都受支持。
使用cross join
但沒有子查詢的變體將得到與@Gordon Linoff
帖子相同的結果,因此您將獲得所有可能的組合
select distinct A.maker, B.type
from product as A cross join product as B
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.