Skip to content

Commit

Permalink
Merge pull request #1105 from yangj1211/cross-join
Browse files Browse the repository at this point in the history
add doc of Cross join
  • Loading branch information
yangj1211 authored Jul 10, 2024
2 parents 23b6a86 + 8f4fefa commit 4f46afd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# **CROSS JOIN**

## **语法说明**

`CROSS JOIN` 用于实现两个表的笛卡尔积,也就是生成两个表中所有行的组合。

## **语法结构**

```
>SELECT column_list
FROM table1
CROSS JOIN table2;
```

## **示例**

```sql
CREATE TABLE Colors (
color_id INT AUTO_INCREMENT,
color_name VARCHAR(50),
PRIMARY KEY (color_id)
);

CREATE TABLE Fruits (
fruit_id INT AUTO_INCREMENT,
fruit_name VARCHAR(50),
PRIMARY KEY (fruit_id)
);

INSERT INTO Colors (color_name) VALUES ('Red'), ('Green'), ('Blue');
INSERT INTO Fruits (fruit_name) VALUES ('Apple'), ('Banana'), ('Cherry');

mysql> SELECT c.color_name, f.fruit_name FROM Colors c CROSS JOIN Fruits f;--生成一个包含所有颜色和所有水果组合的结果集
+------------+------------+
| color_name | fruit_name |
+------------+------------+
| Red | Apple |
| Green | Apple |
| Blue | Apple |
| Red | Banana |
| Green | Banana |
| Blue | Banana |
| Red | Cherry |
| Green | Cherry |
| Blue | Cherry |
+------------+------------+
9 rows in set (0.00 sec)

mysql> SELECT c.color_name,f.fruit_name FROM Colors c CROSS JOIN Fruits f WHERE c.color_name = 'Red' AND f.fruit_name = 'Apple';--筛选出特定颜色和特定水果的组合
+------------+------------+
| color_name | fruit_name |
+------------+------------+
| Red | Apple |
+------------+------------+
1 row in set (0.01 sec)
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ nav:
- SUBQUERY with IN: MatrixOne/Reference/SQL-Reference/Data-Query-Language/subqueries/subquery-with-in.md
- JOIN:
- JOIN 概述: MatrixOne/Reference/SQL-Reference/Data-Query-Language/join/join.md
- CROSS JOIN: MatrixOne/Reference/SQL-Reference/Data-Query-Language/join/cross-join.md
- INNER JOIN: MatrixOne/Reference/SQL-Reference/Data-Query-Language/join/inner-join.md
- LEFT JOIN: MatrixOne/Reference/SQL-Reference/Data-Query-Language/join/left-join.md
- RIGHT JOIN: MatrixOne/Reference/SQL-Reference/Data-Query-Language/join/right-join.md
Expand Down

0 comments on commit 4f46afd

Please sign in to comment.