2017-09-07 34 views
0

我想創建一個查詢,可以輸出多列的每種可能性。輸入如下。創建多個列的每個可能的組合

Keyword 1|Keyword 2|Keyword 3 
---------+---------+---------- 
shoes |buy 
gloves |online 
shirts | 

第二假想輸入將是對1RST以下

Keyword 1|Keyword 2|Keyword 3 
---------+---------+---------- 
shoes |buy  |shop 
gloves |online 
shirts | 

結果如下 鞋買, 手套買, 襯衫團購,網 鞋,在線 手套, 襯衫在線,

第二次的結果如下; 鞋買, 手套買, 襯衫團購,網 鞋,在線 手套,在線 襯衫, 鞋買店, 手套買店, 襯衫買店, 鞋的網店, 手套網店, 襯衫在線商店

有沒有辦法編寫一個查詢,可以在兩種情況下工作?列數將可能改變將高達5

感謝

回答

0

做一個笛卡爾同表之間的連接:

select t1.col, t2.col, t3.col 
from tab t1, tab t2, tab t3 

如果你想消除重複,你可以試試這一招:

select t1.col, t2.col, t3.col 
    from tab t1 
join tab t2 on t1.col != t2.col 
join tab t3 on t2.col != t3.col and t3.col != t1.col 

您可以瞭解不同的選擇在這裏:http://www.postgresqltutorial.com/postgresql-cross-join/

+0

謝謝您的洞察力,是否可以在同一個表中引用此列? –

+0

是的。正如你所看到的,我使用同一個表「tab」中的同一列「col」三次。 –

相關問題