如何将 2 列与 SQL 中所有可能的组合合并?

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

P.S。此查询不会 return 我需要的。

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 post 相同的结果,因此您将获得所有可能的组合

select distinct A.maker, B.type
from product as A cross join product as B