如何获得表的排列?

lokaqttq  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(243)

我有一个10列的表。我想得到所有可能的排列。具有3列的表示例:

col1   col2   col3  
10       40     3,   
20        6     1,

排列表将是:

col1   col2   col3 
10      40      3,   
10      40      1,
10      6       3, 
10      6       1, 
20      6       1,
20      6       3,
20      40      3,
20      40      1,

我试过交叉连接,但没有成功。最好的方法是什么?
谢谢您!

wmtdaxz3

wmtdaxz31#

你可以使用 cross join :

select c1.col1, c2.col2, c3.col3
from (select distinct col1 from t) c1 cross join
     (select distinct col2 from t) c2 cross join
     (select distinct col3 from t) c3
order by c1.col1, c2.col2, c3.col3;

这是一把小提琴。

相关问题