时间:2023-10-27来源:系统城装机大师作者:佚名
内连接就是利用where子句对两种表形成的笛卡尔积进行筛选,之前博客写的查询都是内连接,也是在开发过程中使用的最多的连接查询。
语法:
1 2 3 |
select 字段 from 表1 inner join 表2 on 连接条件 and 其他条件; --或者 select 字段 from 表1 inner join 表2 on 连接条件 where 条件 and 条件; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
--两张数据表 mysql> select * from emp; + --------+--------+-----------+------+---------------------+---------+---------+--------+ | empno | ename | job | mgr | hiredate | sal | comm | deptno | + --------+--------+-----------+------+---------------------+---------+---------+--------+ | 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | | 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | | 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | | 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | | 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | | 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | | 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | | 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | | 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | | 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | | 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | | 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | | 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | | 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | + --------+--------+-----------+------+---------------------+---------+---------+--------+ 14 rows in set (0.07 sec) mysql> select * from dept; + --------+------------+----------+ | deptno | dname | loc | + --------+------------+----------+ | 10 | ACCOUNTING | NEW YORK | | 20 | RESEARCH | DALLAS | | 30 | SALES | CHICAGO | | 40 | OPERATIONS | BOSTON | + --------+------------+----------+ 4 rows in set (0.00 sec) |
之前的写法:
1 2 3 4 5 6 7 |
mysql> select ename,dname from emp,dept where emp.deptno=dept.deptno and ename= 'SMITH' ; + -------+----------+ | ename | dname | + -------+----------+ | SMITH | RESEARCH | + -------+----------+ 1 row in set (0.01 sec) |
标准的内连接的写法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
mysql> select ename,dname from emp inner join dept on emp.deptno=dept.deptno and ename= 'SMITH' ; + -------+----------+ | ename | dname | + -------+----------+ | SMITH | RESEARCH | + -------+----------+ 1 row in set (0.00 sec) mysql> select ename,dname from emp inner join dept on emp.deptno=dept.deptno where ename= 'SMITH' ; + -------+----------+ | ename | dname | + -------+----------+ | SMITH | RESEARCH | + -------+----------+ 1 row in set (0.00 sec) |
外连接分为左外连接和右外连接。
如果联合查询,左侧的表完全显示就是左外连接。
语法:
select 字段名 from 表名1 left join 表名2 on 连接条件;
整两张表:
1 2 3 4 5 6 |
-- 学生表 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); |
1.1查询所有学生的成绩,如果这个学生没有成绩,也要将学生的个人信息显示出来
学生表和成绩表的id并不是一一对应的,如果这里用内连接,未在两个表中出现的id字段将不会被筛选出来:
1 2 3 4 5 6 7 8 |
mysql> select * from stu inner join exam on stu.id=exam.id; + ------+------+------+-------+ | id | name | id | grade | + ------+------+------+-------+ | 1 | jack | 1 | 56 | | 2 | tom | 2 | 76 | + ------+------+------+-------+ 2 rows in set (0.00 sec) |
所以此处需要使用左外连接:
1 2 3 4 5 6 7 8 9 10 |
mysql> select * from stu left join exam on stu.id=exam.id; + ------+------+------+-------+ | id | name | id | grade | + ------+------+------+-------+ | 1 | jack | 1 | 56 | | 2 | tom | 2 | 76 | | 3 | kity | NULL | NULL | | 4 | nono | NULL | NULL | + ------+------+------+-------+ 4 rows in set (0.00 sec) |
如果联合查询,右侧的表完全显示我们就说是右外连接。
语法:
select 字段 from 表名1 right join 表名2 on 连接条件;
2.1对stu表和exam表联合查询,把所有的成绩都显示出来,即使这个成绩没有学生与它对应,也要显示出来
1 2 3 4 5 6 7 8 9 |
mysql> select exam.id, name ,grade from stu right join exam on stu.id=exam.id; + ------+------+-------+ | id | name | grade | + ------+------+-------+ | 1 | jack | 56 | | 2 | tom | 76 | | 11 | NULL | 8 | + ------+------+-------+ 3 rows in set (0.00 sec) |
2.2列出部门名称和这些部门的员工信息,同时列出没有员工的部门
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
mysql> select * from emp right join dept on emp.deptno=dept.deptno order by emp.deptno asc ; + --------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+ | empno | ename | job | mgr | hiredate | sal | comm | deptno | deptno | dname | loc | + --------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+ | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 40 | OPERATIONS | BOSTON | | 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 10 | ACCOUNTING | NEW YORK | | 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 10 | ACCOUNTING | NEW YORK | | 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 10 | ACCOUNTING | NEW YORK | | 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 20 | RESEARCH | DALLAS | | 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 20 | RESEARCH | DALLAS | | 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 20 | RESEARCH | DALLAS | | 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 20 | RESEARCH | DALLAS | | 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 20 | RESEARCH | DALLAS | | 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 30 | SALES | CHICAGO | | 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 30 | SALES | CHICAGO | | 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 30 | SALES | CHICAGO | | 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 30 | SALES | CHICAGO | | 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 30 | SALES | CHICAGO | | 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 30 | SALES | CHICAGO | + --------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+ 15 rows in set (0.00 sec) |
以上就是详解MySQL中表的内外连接的详细内容
2023-10-30
windows上的mysql服务突然消失提示10061 Unkonwn error问题及解决方案2023-10-30
MySQL非常重要的日志bin log详解2023-10-30
详解MySQL事务日志redo log一、单表查询 1、排序 2、聚合函数 3、分组 4、limit 二、SQL约束 1、主键约束 2、非空约束 3、唯一约束 4、外键约束 5、默认值 三、多表查询 1、内连接 1)隐式内连接: 2)显式内连接: 2、外连接 1)左外连接 2)右外连接 四...
2023-10-30
Mysql删除表重复数据 表里存在唯一主键 没有主键时删除重复数据 Mysql删除表中重复数据并保留一条 准备一张表 用的是mysql8 大家自行更改 创建表并添加四条相同的数据...
2023-10-30