内外连接【MySQL】
文章目录
- 表的内外连接
- 内连接
- 外连接
表的内外连接
显示SMITH的名字和部门名称
内连接
第一种写法,用笛卡尔积
mysql> select emp.ename,dept.dname from emp,dept where emp.deptno=dept.deptno ;
+--------+------------+
| ename | dname |
+--------+------------+
| SMITH | RESEARCH |
| ALLEN | SALES |
| WARD | SALES |
| JONES | RESEARCH |
| MARTIN | SALES |
| BLAKE | SALES |
| CLARK | ACCOUNTING |
| SCOTT | RESEARCH |
| KING | ACCOUNTING |
| TURNER | SALES |
| ADAMS | RESEARCH |
| JAMES | SALES |
| FORD | RESEARCH |
| MILLER | ACCOUNTING |
+--------+------------+
14 rows in set (0.00 sec)
内连接
mysql> select ename,dname from emp inner join dept on emp.deptno=dept.deptno ;
+--------+------------+
| ename | dname |
+--------+------------+
| SMITH | RESEARCH |
| ALLEN | SALES |
| WARD | SALES |
| JONES | RESEARCH |
| MARTIN | SALES |
| BLAKE | SALES |
| CLARK | ACCOUNTING |
| SCOTT | RESEARCH |
| KING | ACCOUNTING |
| TURNER | SALES |
| ADAMS | RESEARCH |
| JAMES | SALES |
| FORD | RESEARCH |
| MILLER | ACCOUNTING |
+--------+------------+
14 rows 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)
外连接
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);
例如:查询所有学生的成绩,如果这个学生没有成绩,也要将学生的个人信息显示出来
左外连接:如果联合查询,左侧的表完全显示,右侧的表按条件拼接,条件满足,拼接上,若条件不满足,拼接NULL
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)
右外连接
如果联合查询,右侧的表完全显示我们就说是右外连接
例如:对stu表和exam表联合查询,把所有的成绩都显示出来,即使这个成绩没有学生与它对应,也要
显示出来
mysql> select * from stu right join exam on stu.id=exam.id;
+------+------+------+-------+
| id | name | id | grade |
+------+------+------+-------+
| 1 | jack | 1 | 56 |
| 2 | tom | 2 | 76 |
| NULL | NULL | 11 | 8 |
+------+------+------+-------+
3 rows in set (0.00 sec)
列出部门名称和这些部门的员工信息,同时列出没有员工的部门
1、左外连接
mysql> select * from dept left join emp on dept.deptno=emp.deptno;
+--------+------------+----------+--------+--------+-----------+------+---------------------+---------+---------+--------+
| deptno | dname | loc | empno | ename | job | mgr | hiredate | sal | comm | deptno |
+--------+------------+----------+--------+--------+-----------+------+---------------------+---------+---------+--------+
| 20 | RESEARCH | DALLAS | 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 |
| 30 | SALES | CHICAGO | 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 |
| 30 | SALES | CHICAGO | 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 |
| 20 | RESEARCH | DALLAS | 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 |
| 30 | SALES | CHICAGO | 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 |
| 30 | SALES | CHICAGO | 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 |
| 10 | ACCOUNTING | NEW YORK | 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 |
| 20 | RESEARCH | DALLAS | 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 |
| 10 | ACCOUNTING | NEW YORK | 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 |
| 30 | SALES | CHICAGO | 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 |
| 20 | RESEARCH | DALLAS | 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 |
| 30 | SALES | CHICAGO | 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 |
| 20 | RESEARCH | DALLAS | 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 |
| 10 | ACCOUNTING | NEW YORK | 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 |
| 40 | OPERATIONS | BOSTON | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
+--------+------------+----------+--------+--------+-----------+------+---------------------+---------+---------+--------+
15 rows in set (0.01 sec)
mysql> select * from dept left join emp on dept.deptno=emp.deptno order by dept.deptno asc;
+--------+------------+----------+--------+--------+-----------+------+---------------------+---------+---------+--------+
| deptno | dname | loc | empno | ename | job | mgr | hiredate | sal | comm | deptno |
+--------+------------+----------+--------+--------+-----------+------+---------------------+---------+---------+--------+
| 10 | ACCOUNTING | NEW YORK | 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 |
| 10 | ACCOUNTING | NEW YORK | 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 |
| 20 | RESEARCH | DALLAS | 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 |
| 30 | SALES | CHICAGO | 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 |
| 40 | OPERATIONS | BOSTON | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
+--------+------------+----------+--------+--------+-----------+------+---------------------+---------+---------+--------+
15 rows in set (0.00 sec)
mysql> select dname,ename,dept.deptno from dept left join emp on dept.deptno=emp.deptno order by dept.deptno asc;
+------------+--------+--------+
| dname | ename | deptno |
+------------+--------+--------+
| ACCOUNTING | KING | 10 |
| ACCOUNTING | CLARK | 10 |
| ACCOUNTING | MILLER | 10 |
| RESEARCH | ADAMS | 20 |
| RESEARCH | SCOTT | 20 |
| RESEARCH | FORD | 20 |
| RESEARCH | JONES | 20 |
| RESEARCH | SMITH | 20 |
| SALES | MARTIN | 30 |
| SALES | ALLEN | 30 |
| SALES | JAMES | 30 |
| SALES | BLAKE | 30 |
| SALES | WARD | 30 |
| SALES | TURNER | 30 |
| OPERATIONS | NULL | 40 |
+------------+--------+--------+
15 rows in set (0.00 sec)