概述
INNER JOIN (内连接)
markdown
Table A
ID | Name |
1 | Alice |
2 | Bob |
Table B
ID | Status |
1 | Active |
3 | Inactive |
INNER JOIN 结果
ID | Name | Status |
1 | Alice | Active |
LEFT JOIN (左连接)
markdown
Table A
ID | Name |
1 | Alice |
2 | Bob |
Table B
ID | Status |
1 | Active |
3 | Inactive |
LEFT JOIN 结果
ID | Name | Status |
1 | Alice | Active |
2 | Bob | NULL |
RIGHT JOIN (右连接)
markdown
Table A
ID | Name |
1 | Alice |
2 | Bob |
Table B
ID | Status |
1 | Active |
3 | Inactive |
RIGHT JOIN 结果
ID | Name | Status |
1 | Alice | Active |
3 | NULL | Inactive |
FULL JOIN (全连接)
markdown
Table A
ID | Name |
1 | Alice |
2 | Bob |
Table B
ID | Status |
1 | Active |
3 | Inactive |
FULL JOIN 结果
ID | Name | Status |
1 | Alice | Active |
2 | Bob | NULL |
3 | NULL | Inactive |
CROSS JOIN (交叉连接)
markdown
Table A
ID | Name |
1 | Alice |
2 | Bob |
Table B
ID | Status |
1 | Active |
2 | Inactive |
CROSS JOIN 结果
ID_A | Name | ID_B | Status |
1 | Alice | 1 | Active |
1 | Alice | 2 | Inactive |
2 | Bob | 1 | Active |
2 | Bob | 2 | Inactive |
请注意,在CROSS JOIN的结果中,我添加了ID_A和ID_B来区分来自不同表的ID字段,以避免混淆。在实际应用中,如果两个表中的列名相同,通常需要在查询中重命名至少一个列,以便能够清晰地引用它们。