MariaDB CROSS JOIN 关键字用于返回两个表(表 1 和表 2)中的所有记录。它有时被称为CARTESIAN JOIN,因为在没有 WHERE 条件的情况下,它的行为类似于笛卡尔积,即结果集中的行数是两个表的行数的乘积。
语法
语法在 MariaDB 中使用 CROSS JOIN 关键字的方法如下:
SELECT table1.column1, table1.column2, table2.column1, table2.column2, ...
FROM table1
CROSS JOIN table2;
注意:CROSS JOIN 可能会返回非常大的结果集。
示例:
考虑名为 Employee 和 Contact_Info 的数据库表,其中包含以下记录:
表 1:Employee 表
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
5 | Ramesh | New Delhi | 28 | 3000 |
6 | Huang | Beijing | 28 | 2800 |
表 2:Contact_Info 表
Phone_Number | EmpID | Address | Gender |
---|---|---|---|
+1-8054098000 | 2 | Brooklyn, New York, USA | F |
+33-147996101 | 3 | Grenelle, Paris, France | M |
+31-201150319 | 4 | Geuzenveld, Amsterdam, Netherlands | F |
+86-1099732458 | 6 | Yizhuangzhen, Beijing, China | M |
+65-67234824 | 7 | Yishun, Singapore | M |
+81-357799072 | 8 | Koto City, Tokyo, Japan | M |
要交叉连接 Employee 和 Contact_Info 表,考虑
SELECT Employee.Name, Employee.Age, Contact_Info.Address
FROM Employee
CROSS JOIN Contact_Info;
这将产生如下所示的结果(请注意,产生的结果包含两个表中记录集的每种可能的组合):
Name | Age | Address |
---|---|---|
John | 25 | Brooklyn, New York, USA |
John | 25 | Grenelle, Paris, France |
John | 25 | Geuzenveld, Amsterdam, Netherlands |
John | 25 | Yizhuangzhen, Beijing, China |
John | 25 | Yishun, Singapore |
John | 25 | Koto City, Tokyo, Japan |
Marry | 24 | Brooklyn, New York, USA |
Marry | 24 | Grenelle, Paris, France |
Marry | 24 | Geuzenveld, Amsterdam, Netherlands |
Marry | 24 | Yizhuangzhen, Beijing, China |
Marry | 24 | Yishun, Singapore |
Marry | 24 | Koto City, Tokyo, Japan |
Jo | 27 | Brooklyn, New York, USA |
Jo | 27 | Grenelle, Paris, France |
Jo | 27 | Geuzenveld, Amsterdam, Netherlands |
Jo | 27 | Yizhuangzhen, Beijing, China |
Jo | 27 | Yishun, Singapore |
Jo | 27 | Koto City, Tokyo, Japan |
Kim | 30 | Brooklyn, New York, USA |
Kim | 30 | Grenelle, Paris, France |
Kim | 30 | Geuzenveld, Amsterdam, Netherlands |
Kim | 30 | Yizhuangzhen, Beijing, China |
Kim | 30 | Yishun, Singapore |
Kim | 30 | Koto City, Tokyo, Japan |
Ramesh | 28 | Brooklyn, New York, USA |
Ramesh | 28 | Grenelle, Paris, France |
Ramesh | 28 | Geuzenveld, Amsterdam, Netherlands |
Ramesh | 28 | Yizhuangzhen, Beijing, China |
Ramesh | 28 | Yishun, Singapore |
Ramesh | 28 | Koto City, Tokyo, Japan |
Huang | 28 | Brooklyn, New York, USA |
Huang | 28 | Grenelle, Paris, France |
Huang | 28 | Geuzenveld, Amsterdam, Netherlands |
Huang | 28 | Yizhuangzhen, Beijing, China |
Huang | 28 | Yishun, Singapore |
Huang | 28 | Koto City, Tokyo, Japan |
在 CROSS JOIN 中使用 WHERE / ON 子句
添加 WHERE / ON 子句(指定两个表之间的关系),CROSS JOIN 将产生与 INNER JOIN 子句。
语法
使用 CROSS JOIN 的语法MariaDB 中的 WHERE / ON 子句如下:
/* 使用ON子句 */
SELECT table1.column1, table1.column2, table2.column1, table2.column2, ...
FROM table1
CROSS JOIN table2
ON table1.matching_column = table2.matching_column;
/* 使用WHERE子句 */
SELECT table1.column1, table1.column2, table2.column1, table2.column2, ...
FROM table1
CROSS JOIN table2
WHERE table1.matching_column = table2.matching_column;
示例:
考虑上面讨论的数据库表 Employee 和 Contact_Info.
根据匹配列交叉连接 Employee 和 Contact_Info 表 EmpID,可以使用以下查询:
SELECT Employee.Name, Employee.Age, Contact_Info.Address
FROM Employee
CROSS JOIN Contact_Info
ON Employee.EmpID = Contact_Info.EmpID;
这将产生如下所示的结果:
Name | Age | Address |
---|---|---|
Marry | 24 | Brooklyn, New York, USA |
Jo | 27 | Grenelle, Paris, France |
Kim | 30 | Geuzenveld, Amsterdam, Netherlands |
Huang | 28 | Yizhuangzhen, Beijing, China |