sql 连接查询

1. 什么是连接查询
    1. 查询的时候只从一张表中查询叫做单表查询
    2. 在实际开发中数据并不是存储在一张表中,是同时存储到多张表中的,
    3.这些表和表之间存在关系,我们查询查询的时候通常需要将多个表联合起来查询,并取得数据,这种查询叫做连接查询或者跨表查询!
2. 连接查询的时候,通常会给表取别名,这样效率更高
3. 案例 :查询所有员工和对应的部门
// emp 员工表  dept 部门表
// SQL 92 语法 内连接 等值连接
mysql> select e.ename,d.dname from emp e,dept d where e.deptno = d.deptno;
// SQL 99 语法 内连接 等值连接  表连接独立,结构更清晰
mysql> select e.ename,d.dname from emp e join dept d on e.deptno = d.deptno;
+---------+-------------+
| ename   | dname       |
+---------+-------------+
| SIMITH  | RESEARCHING |
| ALLEN   | SALES       |
| WARD    | SALES       |
| JONES   | RESEARCHING |
| MARTIN  | SALES       |
| BLAKE   | SALES       |
| CLARK   | ACCOUNTING  |
| SCOTT   | RESEARCHING |
| KING    | ACCOUNTING  |
| TURNER  | SALES       |
| ADAMS   | RESEARCHING |
| JAMES   | SALES       |
| FORD    | RESEARCHING |
| MILLER  | ACCOUNTING  |
+---------+-------------+
14 rows in set (0.00 sec)
4.案例 :找出每个一个员工对应的工资等级 要求显示员工名字 员工薪水 员工等级
// emp 员工表  salgrade 等级表
mysql> select e.ename,e.sal ,s.grade from emp e join salgrade s on e.sal between s.losal and s.hisal;
+---------+------+-------+
| ename   | sal  | grade |
+---------+------+-------+
| SIMITH  |  800 |     1 |
| ALLEN   | 1600 |     3 |
| WARD    | 1250 |     2 |
| JONES   | 2975 |     4 |
| MARTIN  | 1250 |     2 |
| BLAKE   | 2850 |     4 |
| CLARK   | 2450 |     4 |
| SCOTT   | 3000 |     4 |
| KING    | 5000 |     5 |
| TURNER  | 1500 |     3 |
| ADAMS   | 1100 |     1 |
| JAMES   |  950 |     1 |
| FORD    | 3000 |     4 |
| MILLER  | 1300 |     2 |
+---------+------+-------+
14 rows in set (0.00 sec)
5. 找出每个员工的上级领导,要求显示员工名字以及对应的领导名字
// SQL 99 语法 内连接中的自连接   把一个表看成两个表查询
mysql> select a.ename empname,b.ename leadername from emp a join emp b on a.mgr = b.empno;
+---------+------------+
| empname | leadername |
+---------+------------+
| SIMITH  | FORD       |
| ALLEN   | BLAKE      |
| WARD    | BLAKE      |
| JONES   | KING       |
| MARTIN  | BLAKE      |
| BLAKE   | KING       |
| CLARK   | KING       |
| SCOTT   | JONES      |
| TURNER  | BLAKE      |
| ADAMS   | SCOTT      |
| JAMES   | BLAKE      |
| FORD    | JONES      |
| MILLER  | CLARK      |
+---------+------------+
13 rows in set (0.00 sec)
6.案例 :找出每个员工对应的部门,要求部门名字全部显示
// 外连接把一张表的记录全部查询出来,对方表没有匹配的用null表示
// 把join 左边全部查询出来使用 left 左连接 ,如果是右边 使用right 有连接
// 右外连接 查询     (同时还有左外连接)
mysql> select e.ename,d.dname from emp e right join dept d on e.deptno = d.deptno;
+---------+-------------+
| ename   | dname       |
+---------+-------------+
| SIMITH  | RESEARCHING |
| ALLEN   | SALES       |
| WARD    | SALES       |
| JONES   | RESEARCHING |
| MARTIN  | SALES       |
| BLAKE   | SALES       |
| CLARK   | ACCOUNTING  |
| SCOTT   | RESEARCHING |
| KING    | ACCOUNTING  |
| TURNER  | SALES       |
| ADAMS   | RESEARCHING |
| JAMES   | SALES       |
| FORD    | RESEARCHING |
| MILLER  | ACCOUNTING  |
| NULL    | OPERATIONS  |
+---------+-------------+
15 rows in set (0.01 sec)
7. 案例 :找出每个员工对应的部门名称,以及员工对应的薪水等级,要求显示员工名字 部门名字 薪水等级
// 三张表查询(emp) (dept) (salgrade)
mysql> select e.ename,d.dname,s.grade from emp e join dept d on e.deptno = d.deptno join salgrade s on e.sal between s.losal and s.hisal;
// emp 先和 dept 比较,然后 emp 再和 salgrade 比较
+---------+-------------+-------+
| ename   | dname       | grade |
+---------+-------------+-------+
| SIMITH  | RESEARCHING |     1 |
| ALLEN   | SALES       |     3 |
| WARD    | SALES       |     2 |
| JONES   | RESEARCHING |     4 |
| MARTIN  | SALES       |     2 |
| BLAKE   | SALES       |     4 |
| CLARK   | ACCOUNTING  |     4 |
| SCOTT   | RESEARCHING |     4 |
| KING    | ACCOUNTING  |     5 |
| TURNER  | SALES       |     3 |
| ADAMS   | RESEARCHING |     1 |
| JAMES   | SALES       |     1 |
| FORD    | RESEARCHING |     4 |
| MILLER  | ACCOUNTING  |     2 |
+---------+-------------+-------+
14 rows in set (0.00 sec)



你可能感兴趣的:(sql 连接查询)