多表查询
一:表的基本介绍
    可以参考:https://www.cnblogs.com/cdf-opensource-007/p/6517627.html
建立一个员工表信息表和一个部门表,每个员工都对应在哪个部门。因此这两张表具有一定关系。需要将两个表格进行查询,找到一个
员工所对应的所属部门。
01:建立一个员工表:employee 和一个部门表departmentemployee表:
  mysql> create table employee(
-> id int primary key auto_increment,
-> name varchar(20),
-> sex enum("male","female") not null default "male",
-> age int,
-> dep_id int
-> );
Query OK, 0 rows affected (0.18 sec) department表:
mysql> create table department(
-> id int,
-> name varchar(20)
-> );
Query OK, 0 rows affected (0.12 sec)
 02:给两张表加入信息。
employee表:
mysql> insert into employee(name,sex,age,dep_id) values
-> ("egon","male",18,200),
-> ("alex","female",48,201),
-> ("wupeiqi","male",38,210),
-> ("yuanhao","female",28,202),
-> ("liwenzhou","female",28,202),
-> ("jingwen","female",18,204);
Query OK, 6 rows affected (0.05 sec)
Records: 6 Duplicates: 0 Warnings: 0

 查看employee的结构:

mysql> desc employee;
+--------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| sex | enum('male','female') | NO | | male | |
| age | int(11) | YES | | NULL | |
| dep_id | int(11) | YES | | NULL | |
+--------+-----------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

查看employee的信息:


mysql> select * from employee;
+----+-----------+--------+------+--------+
| id | name | sex | age | dep_id |
+----+-----------+--------+------+--------+
| 1 | egon | male | 18 | 200 |
| 2 | alex | female | 48 | 201 |
| 3 | wupeiqi | male | 38 | 210 |
| 4 | yuanhao | female | 28 | 202 |
| 5 | liwenzhou | female | 28 | 202 |
| 6 | jingwen | female | 18 | 204 |
+----+-----------+--------+------+--------+
6 rows in set (0.00 sec)
department表:
mysql> insert into department values
-> (200,"技术"),
-> (201,"人力资源"),
-> (202,"销售"),
-> (203,"运营");
Query OK, 4 rows affected (0.04 sec)
Records: 4 Duplicates: 0 Warnings: 0 department的结构:
mysql> desc department;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
 department的信息:
mysql> select * from department;
+------+--------------+
| id | name |
+------+--------------+
| 200 | 技术 |
| 201 | 人力资源 |
| 202 | 销售 |
| 203 | 运营 |
+------+--------------+
4 rows in set (0.00 sec)

二:多表查询(以employee和department为例)

#重点:外链接语法:   select 字段列表 from 表1 inner/left/right join 表2  on  表1.字段=表2.字段;
01:交叉链接:生成笛卡儿积:
  

mysql> select * from employee,department;
+----+-----------+--------+------+--------+------+--------------+
| id | name | sex | age | dep_id | id | name |
+----+-----------+--------+------+--------+------+--------------+
| 1 | egon | male | 18 | 200 | 200 | 技术 |
| 1 | egon | male | 18 | 200 | 201 | 人力资源 |
| 1 | egon | male | 18 | 200 | 202 | 销售 |
| 1 | egon | male | 18 | 200 | 203 | 运营 |
| 2 | alex | female | 48 | 201 | 200 | 技术 |
| 2 | alex | female | 48 | 201 | 201 | 人力资源 |
| 2 | alex | female | 48 | 201 | 202 | 销售 |
| 2 | alex | female | 48 | 201 | 203 | 运营 |
| 3 | wupeiqi | male | 38 | 210 | 200 | 技术 |
| 3 | wupeiqi | male | 38 | 210 | 201 | 人力资源 |
| 3 | wupeiqi | male | 38 | 210 | 202 | 销售 |
| 3 | wupeiqi | male | 38 | 210 | 203 | 运营 |
| 4 | yuanhao | female | 28 | 202 | 200 | 技术 |
| 4 | yuanhao | female | 28 | 202 | 201 | 人力资源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 销售 |
| 4 | yuanhao | female | 28 | 202 | 203 | 运营 |
| 5 | liwenzhou | female | 28 | 202 | 200 | 技术 |
| 5 | liwenzhou | female | 28 | 202 | 201 | 人力资源 |
| 5 | liwenzhou | female | 28 | 202 | 202 | 销售 |
| 5 | liwenzhou | female | 28 | 202 | 203 | 运营 |
| 6 | jingwen | female | 18 | 204 | 200 | 技术 |
| 6 | jingwen | female | 18 | 204 | 201 | 人力资源 |
| 6 | jingwen | female | 18 | 204 | 202 | 销售 |
| 6 | jingwen | female | 18 | 204 | 203 | 运营 |
+----+-----------+--------+------+--------+------+--------------+
24 rows in set (0.00 sec)
这种显示是将所有列表排序都生成了,我们要找的排序肯定会在这个笛卡儿积表格中,但是不利于分析。
02:内链接,只链接匹配的行。
  

mysql> select * from employee,department where department.id=employee.dep_id;  #where条件分析。
+----+-----------+--------+------+--------+------+--------------+
| id | name | sex | age | dep_id | id | name |
+----+-----------+--------+------+--------+------+--------------+
| 1 | egon | male | 18 | 200 | 200 | 技术 |
| 2 | alex | female | 48 | 201 | 201 | 人力资源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 销售 |
| 5 | liwenzhou | female | 28 | 202 | 202 | 销售 |
+----+-----------+--------+------+--------+------+--------------+
4 rows in set (0.00 sec)
#再看一个需求,我要查出技术部的员工的名字
  mysql> select name from employee,department where employee.dep_id=department.id and department.name='技术';
ERROR 1052 (23000): Column 'name' in field list is ambiguous
#上面直接就报错了,因为select后面直接写的name,在两个表合并起来的表中,是有两个name字段的,直接写name是不行的,要加上表名,再看:
mysql> select employee.name from employee,department where employee.dep_id=department.id and department.name="技术";
+------+
| name |
+------+
| egon |
+------+
1 row in set (0.00 sec)

03:外链接之左链接:优先显示左边表的全部记录

  #以左表为准,即找出所有员工信息,当然包括没有部门的员工
#本质就是:在内链接的基础上增加左边有而右边没有的结果
mysql> select employee.id,employee.name,department.name as depart_name from employee
> left join department on employee.dep_id=department.id;
+----+-----------+--------------+
| id | name | depart_name |
+----+-----------+--------------+
| 1 | egon | 技术 |
| 2 | alex | 人力资源 |
| 4 | yuanhao | 销售 |
| 5 | liwenzhou | 销售 |
| 3 | wupeiqi | NULL |
| 6 | jingwen | NULL |
+----+-----------+--------------+
6 rows in set (0.00 sec)
04:外链接之右链接:优先显示右边表的全部记录
  #以右表为准,即找出所有员工信息,当然包括没有部门的员工
#本质就是:在内链接的基础上增加右边有而左边没有的结果
mysql> select employee.id,employee.name,department.name as depart_name from
-> employee right join department on
-> employee.dep_id=department.id;
+------+-----------+--------------+
| id | name | depart_name |
+------+-----------+--------------+
| 1 | egon | 技术 |
| 2 | alex | 人力资源 |
| 4 | yuanhao | 销售 |
| 5 | liwenzhou | 销售 |
| NULL | NULL | 运营 |
+------+-----------+--------------+
5 rows in set (0.00 sec)
05:全外链接:显示左右两个表的全部内容
001:union显示的左右两边的数据,并将重复的数据去重。
      

mysql> select * from employee left join department on employee.dep_id=department.id
-> union
-> select * from employee right join department on employee.dep_id=department.id
-> ;
+------+-----------+--------+------+--------+------+--------------+
| id | name | sex | age | dep_id | id | name |
+------+-----------+--------+------+--------+------+--------------+
| 1 | egon | male | 18 | 200 | 200 | 技术 |
| 2 | alex | female | 48 | 201 | 201 | 人力资源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 销售 |
| 5 | liwenzhou | female | 28 | 202 | 202 | 销售 |
| 3 | wupeiqi | male | 38 | 210 | NULL | NULL |
| 6 | jingwen | female | 18 | 204 | NULL | NULL |
| NULL | NULL | NULL | NULL | NULL | 203 | 运营 |
+------+-----------+--------+------+--------+------+--------------+
7 rows in set (0.06 sec)
    002:union all  显示的是左右两边表的数据,并不会去重
    

mysql> select * from employee left join department on employee.dep_id=department.id
-> union all
-> select * from employee right join department on employee.dep_id=department.id;
+------+-----------+--------+------+--------+------+--------------+
| id | name | sex | age | dep_id | id | name |
+------+-----------+--------+------+--------+------+--------------+
| 1 | egon | male | 18 | 200 | 200 | 技术 |
| 2 | alex | female | 48 | 201 | 201 | 人力资源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 销售 |
| 5 | liwenzhou | female | 28 | 202 | 202 | 销售 |
| 3 | wupeiqi | male | 38 | 210 | NULL | NULL |
| 6 | jingwen | female | 18 | 204 | NULL | NULL |
| 1 | egon | male | 18 | 200 | 200 | 技术 |
| 2 | alex | female | 48 | 201 | 201 | 人力资源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 销售 |
| 5 | liwenzhou | female | 28 | 202 | 202 | 销售 |
| NULL | NULL | NULL | NULL | NULL | 203 | 运营 |
+------+-----------+--------+------+--------+------+--------------+
11 rows in set (0.00 sec)
三:符合条件接连的查询
1、
#示例1:以内连接的方式查询employee和department表,并且employee表中的age字段值必须大于25,即找出年龄大于25岁的员工以及员工所在的部门
mysql> select employee.name,department.name from employee inner join department
-> on employee.dep_id=department.id
-> where age>25;
+-----------+--------------+
| name | name |
+-----------+--------------+
| alex | 人力资源 |
| yuanhao | 销售 |
| liwenzhou | 销售 |
+-----------+--------------+
3 rows in set (0.00 sec)
2、#实例:以内连接的方式查询employee和department表,并且以age字段的生序方式显示;
  

实例01:内连接的方式查询employee和department表,并且employee表中的age字段值必须大于25,即找出年龄大于25岁的员工以及员工所在的部门
mysql> select employee.name,department.name from employee inner join department
-> on employee.dep_id=department.id where age>25
-> order by age; #(order by 排序是默认的升序的,升序order by asc)
+-----------+--------------+
| name | name |
+-----------+--------------+
| yuanhao | 销售 |
| liwenzhou | 销售 |
| alex | 人力资源 |
+-----------+--------------+
3 rows in set (0.00 sec)

实例一

  

实例02:以内连接的方式查询employee和department表,并且以age字段的升序方式显示
mysql> select employee.id,employee.name,employee.age,department.name from employee,department
-> where employee.dep_id=department.id
-> and age >25
-> order by age asc;
+----+-----------+------+--------------+
| id | name | age | name |
+----+-----------+------+--------------+
| 5 | liwenzhou | 28 | 销售 |
| 4 | yuanhao | 28 | 销售 |
| 2 | alex | 48 | 人力资源 |
+----+-----------+------+--------------+
3 rows in set (0.00 sec)

实例二

四:子查询
解释:子查询就是将一个查询结果用括号括起来,交给另外一个sql语句,作为它的一个查询语句来进行操作。
子查询:
#1:子查询是将一个查询语句嵌套在另一个查询语句中。
#2:内层查询语句的查询结果,可以为外层查询语句提供查询条件。
#3:子查询中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等关键字
#4:还可以包含比较运算符:= 、 !=、> 、<等
  1: 带in关键字的的字查询:
 mysql> select id,name from department where id in (select dep_id from employee group by
-> dep_id having avg(age)>25);
+------+--------------+
| id | name |
+------+--------------+
| 201 | 人力资源 |
| 202 | 销售 |
+------+--------------+
2 rows in set (0.05 sec) 总结:子查询的思路和解决问题一样,先解决一个然后拿着这个的结果再去解决另外一个问题,连表的思路是先将两个表关联在一起,
然后在进行group by啊过滤啊等等操作,两者的思路是不一样的.
  

01:#查询员工平均年龄在25岁以上的部门名,可以用连表,也可以用子查询,
mysql> select department.name from department inner join employee on department.id=employee.dep_id
-> group by department.name having avg(age)>25;
+--------------+
| name |
+--------------+
| 人力资源 |
| 销售 |
+--------------+

案例01

  

02:#查看技术部员工姓名
mysql> select name from employee where dep_id in (select id from department where name="技术");
+------+
| name |
+------+
| egon |
+------+
1 row in set (0.00 sec)

案例02

  

03:#查看不足1人的部门名(子查询得到的是有人的部门id)
mysql> select id,name from department where id not in (select distinct dep_id from employee);
+------+--------+
| id | name |
+------+--------+
| 203 | 运营 |
+------+--------+
1 row in set (0.00 sec)

案例03

2:比较运算符的子查询:
#比较运算符:=、!=、>、>=、<、<=、<>
#查询大于所有人平均年龄的员工名与年龄
mysql> select name,age from employee where age >(select avg(age) from employee);
+---------+------+
| name | age |
+---------+------+
| alex | 48 |
| wupeiqi | 38 |
+---------+------+
2 rows in set (0.00 sec)
  3:带exists(存在)关键字的子查询
  EXISTS关字键字表示存在。在使用EXISTS关键字时,内层查询语句不返回查询的记录。而是返回一个真假值。True或False
  当返回True时,外层查询语句将进行查询;当返回值为False时,外层查询语句不进行查询。还可以写not exists,和exists的效果就是反的
    01:employee表中不存在id为200    
   mysql> select * from employee where exists (select id from employee where id=500);
Empty set (0.00 sec) 
    02:employee存在的情况:
  #只有当括号里面的条件成立了,select * from employee  才会执行
mysql> select * from employee where exists (select dep_id from employee where dep_id=200);
+----+-----------+--------+------+--------+
| id | name | sex | age | dep_id |
+----+-----------+--------+------+--------+
| 1 | egon | male | 18 | 200 |
| 2 | alex | female | 48 | 201 |
| 3 | wupeiqi | male | 38 | 210 |
| 4 | yuanhao | female | 28 | 202 |
| 5 | liwenzhou | female | 28 | 202 |
| 6 | jingwen | female | 18 | 204 |
+----+-----------+--------+------+--------+
6 rows in set (0.00 sec)


day--39-MySQL的多表查询的更多相关文章

  1. day 39 MySQL之多表查询

    MySQL之多表查询   阅读目录 一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习 一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 首先说一下,我们写项目一般都 ...

  2. Mariadb/MySQL数据库单表查询基本操作及DML语句

    Mariadb/MySQL数据库单表查询基本操作及DML语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一数据库及表相关概述 1>.数据库操作 创建数据库: CREATE ...

  3. Vc数据库编程基础MySql数据库的表查询功能

    Vc数据库编程基础MySql数据库的表查询功能 一丶简介 不管是任何数据库.都会有查询功能.而且是很重要的功能.上一讲知识简单的讲解了表的查询所有. 那么这次我们需要掌握的则是. 1.使用select ...

  4. MySQL之多表查询一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习

    MySQL之多表查询 阅读目录 一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习 一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 首先说一下,我们写项目一般都会建 ...

  5. MySQL之单表查询 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER BY 八 限制查询的记录数:LIMIT 九 使用正则表达式查询

    MySQL之单表查询 阅读目录 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER B ...

  6. day15(mysql 的多表查询,事务)

    mysql之多表查询 1.合并结果集 作用:合并结果集就是把两个select语句查询的结果连接到一起! /*创建表t1*/ CREATE TABLE t1( a INT PRIMARY KEY , b ...

  7. mysql数据库优化课程---11、mysql普通多表查询

    mysql数据库优化课程---11.mysql普通多表查询 一.总结 一句话总结:select user.username,user.age,class.name,class.ctime from u ...

  8. day 38 MySQL之单表查询

    MySQL之单表查询   阅读目录 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER ...

  9. MySQL的联表查询

    MySQL的联表查询 首选:分析查询的字段来自哪些表 进而:确定交集 然后:确定判断的条件 比如:从student表 和 result表 查学号.考试名称.学时.考试日期.考试成绩 表1: 学号 考试 ...

  10. Mysql数据库多表查询

    一.介绍 首先说一下,我们写项目一般都会建一个数据库,那数据库里面是不是存了好多张表啊,不可能把所有的数据都放到一张表里面,肯定要分表来存数据,这样节省空间,数据的组织结构更清晰,解耦和程度更高,但是 ...

随机推荐

  1. Collection Types

    [Collection Types] 1.Arrays store ordered lists of values of the same type. Value必须是同一类型. 2.Array的原型 ...

  2. iframe 模拟ajax文件上传and formdata ajax 文件上传

    对于文件上传 有好多种方式,一直想总结 文件上传的方法 今天就来写下 iframe  的文件上传的代码 本人语言表达能里有限,不多说了 直接上代码. 首先看 总体页面. 总共就三个文件. 实际上也就是 ...

  3. js的两种查询方式 LHS and RHS

    为了进一步理解,我们需要多介绍一点编译器的术语.编译器在编译过程的第二步中生成了代码,引擎执行它时,会通过查找变量 a 来判断它是否已声明过.查找的过程由作用域进行协助,但是引擎执行怎样的查找,会影响 ...

  4. The 'Microsoft Jet OLEDB 4.0 Provider' is not registered on the local machine

    在一台Win7 64位的操纵系统上部署的C# Web系统,操作Excel,批量导入数据,报错,提示错误信息: The ‘Microsoft Jet OLEDB 4.0 Provider' is not ...

  5. Spring 实例化bean的三种方式

    第一种方法:直接配置Bean <bena id="所需要实例化的一个实例名称" class="包名.类名"/> 例如: 配置文件中的bean.XML ...

  6. ROS naviagtion analysis: costmap_2d--LayeredCostmap

    博客转自:https://blog.csdn.net/u013158492/article/details/50490490 在数据成员中,有两个重要的变量:Costmap2D costmap_和 s ...

  7. Android 单例模式探讨

    Singleton模式可以是很简单的,它的全部只需要一个类就可以完成(看看这章可怜的UML图).但是如果在“对象创建的次数以及何时被创建”这两点上较真起来,Singleton模式可以相当的复杂,比头五 ...

  8. Windows Cmder

    一.简介 作为一个程序员,即使是在windows工作环境,cmd也是我们必不可少的使用工具.cmder 是为 Windows 提供的一个便携式控制台仿真器,用来替代windows的cmd,使用非常简单 ...

  9. Linux下chmod 777 修改权限

    在linux操作系统下,使用shell命令来操作: 关于权限的问题用chmod命令来修改权限 -rw-r-r-- 1 root root 可参考:http://zhidao.baidu.com/lin ...

  10. Requests接口测试(三)

    一.定制请求头 我们先来看一下,关于请求头的定制演示,相信了解http协议的小伙伴应该对请求头部headers请求头部并不陌生,那么作为实际工作中的我们,如果想自定义一些请求头的信息,我们应该怎么办呢 ...