📅  最后修改于: 2020-11-11 09:37:36             🧑  作者: Mango
当第一张表的每一行与第二张表的每一行合并时,称为笛卡尔联接或交叉联接。一般而言,我们可以说SQL CROSS JOIN返回联接表中行集的笛卡尔积。
我们可以通过两种方式指定CROSS JOIN:
SQL Cross Join的语法
SELECT * FROM [TABLE1] CROSS JOIN [TABLE2]
OR
SELECT * FROM [ TABLE1] , [TABLE2]
让我们以两个表为例,
表1-MatchScore
Player | Department_id | Goals |
---|---|---|
Franklin | 1 | 2 |
Alan | 1 | 3 |
Priyanka | 2 | 2 |
Rajesh | 3 | 5 |
表2-部门
Department_id | Department_name |
---|---|
1 | IT |
2 | HR |
3 | Marketing |
SQL语句:
SELECT * FROM MatchScore CROSS JOIN Departments
执行完该查询后,您将发现以下结果:
Player | Department_id | Goals | Depatment_id | Department_name |
---|---|---|---|---|
Franklin | 1 | 2 | 1 | IT |
Alan | 1 | 3 | 1 | IT |
Priyanka | 2 | 2 | 1 | IT |
Rajesh | 3 | 5 | 1 | IT |
Franklin | 1 | 2 | 2 | HR |
Alan | 1 | 3 | 2 | HR |
Priyanka | 2 | 2 | 2 | HR |
Rajesh | 3 | 5 | 2 | HR |
Franklin | 1 | 2 | 3 | Marketing |
Alan | 1 | 3 | 3 | Marketing |
Priyanka | 2 | 2 | 3 | Marketing |
Rajesh | 3 | 5 | 3 | Marketing |