表的连接分为内连和外连
内连接
内连接实际上就是利用where子句对两种表形成的笛卡儿积进行筛选,我们前面学习的查询都是内连接,也是在开发过程中使用的最多的连接查询。
语法:
select 字段 from 表1 inner join 表2 on 连接条件 and 其他条件;
- 显示SMITH的名字和部门名称
-- 用前面的写法
select ename, dname from EMP, DEPT where EMP.deptno=DEPT.deptno and
ename='SMITH';
![![[Pasted image 20260508194139.png]]](https://i-blog.csdnimg.cn/direct/97356def3d2a4f4e8e4a1b80b75f3018.png)
![![[Pasted image 20260508194153.png]]](https://i-blog.csdnimg.cn/direct/a730db2295104f92855b0be8236ea04d.png)
-- 用标准的内连接写法
select ename, dname from EMP inner join DEPT on EMP.deptno=DEPT.deptno and
ename='SMITH';
![![[Pasted image 20260508194217.png]]](https://i-blog.csdnimg.cn/direct/86f44d934bba4f2fbdc2074645bbe561.png)
![![[Pasted image 20260508194233.png]]](https://i-blog.csdnimg.cn/direct/46b12969088c45e3b8e2447c02c22d17.png)
外连接
外连接分为左外连接和右外连接
左外连接
如果联合查询,左侧的表完全显示我们就说是左外连接。
语法:
select 字段名 from 表名1 left join 表名2 on 连接条件
-- 建两张表
create table stu (id int, name varchar(30)); -- 学生表
insert into stu values(1,'jack'),(2,'tom'),(3,'kity'),(4,'nono');
create table exam (id int, grade int); -- 成绩表
insert into exam values(1, 56),(2,76),(11, 8);
![![[Pasted image 20260508194332.png]]](https://i-blog.csdnimg.cn/direct/0cafea997e2244c4b883df1c5c9fcb5f.png)
![![[Pasted image 20260508194405.png]]](https://i-blog.csdnimg.cn/direct/1a6b066a5b0e4bc39bc9f2c4c59e9fe3.png)
![![[Pasted image 20260508194420.png]]](https://i-blog.csdnimg.cn/direct/93c1ae8356954e5ea5666f1efadd31c4.png)
![![[Pasted image 20260508194609.png]]](https://i-blog.csdnimg.cn/direct/025431497b2e421ca9a614b220d8107e.png)
![![[Pasted image 20260508194635.png]]](https://i-blog.csdnimg.cn/direct/3ed7e43d2dfd49d0b297e5e5b5baee09.png)
右外连接
如果联合查询,右侧的表完全显示我们就说是右外连接。
语法:
select 字段 from 表名1 right join 表名2 on 连接条件;
![![[Pasted image 20260508194434.png]]](https://i-blog.csdnimg.cn/direct/1563ef9ad21f4c7384a284765920e01a.png)
398



被折叠的 条评论
为什么被折叠?



