DB笛卡尔积在同一张桌子上

时间:2015-08-07 09:16:36

标签: sql database join cartesian

我有一个包含三行的列表,如下所示:

COL1

TEAM1
TEAM2
team3
team4

我想按照以下结果进行自笛式加入:
team1,team2
team1,team3
team1,team4
team2,team3
team2,team4
team3,team4

2 个答案:

答案 0 :(得分:3)

在数据库术语中,

cartesian productcross join,您可以删除where子句中团队相等的行:

select
    t1.col1, t2.col1
from teams as t1
    cross join teams as t2
where
    t1.col1 <> t2.col1

答案 1 :(得分:1)

您可以将两个表连接在一起以获得预期的输出,如下所示:

select t1.col1, t2.col1
from table t1
join table t2
on t1.col1 <> t2.col1