准备                                                           

建表和数据

  1. #建表
  2. create table department(
  3. id int,
  4. name varchar(20)
  5. );
  6.  
  7. create table employee(
  8. id int primary key auto_increment,
  9. name varchar(20),
  10. sex enum('male','female') not null default 'male',
  11. age int,
  12. dep_id int
  13. );
  14.  
  15. #插入数据
  16. insert into department values
  17. (200,'技术'),
  18. (201,'人力资源'),
  19. (202,'销售'),
  20. (203,'运营');
  21.  
  22. insert into employee(name,sex,age,dep_id) values
  23. ('egon','male',18,200),
  24. ('alex','female',48,201),
  25. ('wupeiqi','male',38,201),
  26. ('yuanhao','female',28,202),
  27. ('liwenzhou','male',18,200),
  28. ('jingliyang','female',18,204)
  29. ;
  30.  
  31. #查看表结构和数据
  32. mysql> desc department;
  33. +-------+-------------+------+-----+---------+-------+
  34. | Field | Type | Null | Key | Default | Extra |
  35. +-------+-------------+------+-----+---------+-------+
  36. | id | int(11) | YES | | NULL | |
  37. | name | varchar(20) | YES | | NULL | |
  38. +-------+-------------+------+-----+---------+-------+
  39.  
  40. mysql> desc employee;
  41. +--------+-----------------------+------+-----+---------+----------------+
  42. | Field | Type | Null | Key | Default | Extra |
  43. +--------+-----------------------+------+-----+---------+----------------+
  44. | id | int(11) | NO | PRI | NULL | auto_increment |
  45. | name | varchar(20) | YES | | NULL | |
  46. | sex | enum('male','female') | NO | | male | |
  47. | age | int(11) | YES | | NULL | |
  48. | dep_id | int(11) | YES | | NULL | |
  49. +--------+-----------------------+------+-----+---------+----------------+
  50.  
  51. mysql> select * from department;
  52. +------+--------------+
  53. | id | name |
  54. +------+--------------+
  55. | 200 | 技术 |
  56. | 201 | 人力资源 |
  57. | 202 | 销售 |
  58. | 203 | 运营 |
  59. +------+--------------+
  60.  
  61. mysql> select * from employee;
  62. +----+------------+--------+------+--------+
  63. | id | name | sex | age | dep_id |
  64. +----+------------+--------+------+--------+
  65. | 1 | egon | male | 18 | 200 |
  66. | 2 | alex | female | 48 | 201 |
  67. | 3 | wupeiqi | male | 38 | 201 |
  68. | 4 | yuanhao | female | 28 | 202 |
  69. | 5 | liwenzhou | male | 18 | 200 |
  70. | 6 | jingliyang | female | 18 | 204 |
  71. +----+------------+--------+------+--------+
  72.  
  73. departmentemployee

内容

                                                                  多表连接查询                                                   

  1. #重点:外连接语句
  2.  
  3. select 字段列表 from 表一 inner|left|right join 表二 on 表一.字段=表二.字段;

1.交叉连接: 不适用任何匹配条件.生成笛卡尔积

  1. mysql> select * from employee,department;
  2. +----+------------+--------+------+--------+------+--------------+
  3. | id | name | sex | age | dep_id | id | name |
  4. +----+------------+--------+------+--------+------+--------------+
  5. | 1 | egon | male | 18 | 200 | 200 | 技术 |
  6. | 1 | egon | male | 18 | 200 | 201 | 人力资源 |
  7. | 1 | egon | male | 18 | 200 | 202 | 销售 |
  8. | 1 | egon | male | 18 | 200 | 203 | 运营 |
  9. | 2 | alex | female | 48 | 201 | 200 | 技术 |
  10. | 2 | alex | female | 48 | 201 | 201 | 人力资源 |
  11. | 2 | alex | female | 48 | 201 | 202 | 销售 |
  12. | 2 | alex | female | 48 | 201 | 203 | 运营 |
  13. | 3 | wupeiqi | male | 38 | 201 | 200 | 技术 |
  14. | 3 | wupeiqi | male | 38 | 201 | 201 | 人力资源 |
  15. | 3 | wupeiqi | male | 38 | 201 | 202 | 销售 |
  16. | 3 | wupeiqi | male | 38 | 201 | 203 | 运营 |
  17. | 4 | yuanhao | female | 28 | 202 | 200 | 技术 |
  18. | 4 | yuanhao | female | 28 | 202 | 201 | 人力资源 |
  19. | 4 | yuanhao | female | 28 | 202 | 202 | 销售 |
  20. | 4 | yuanhao | female | 28 | 202 | 203 | 运营 |
  21. | 5 | liwenzhou | male | 18 | 200 | 200 | 技术 |
  22. | 5 | liwenzhou | male | 18 | 200 | 201 | 人力资源 |
  23. | 5 | liwenzhou | male | 18 | 200 | 202 | 销售 |
  24. | 5 | liwenzhou | male | 18 | 200 | 203 | 运营 |
  25. | 6 | jingliyang | female | 18 | 204 | 200 | 技术 |
  26. | 6 | jingliyang | female | 18 | 204 | 201 | 人力资源 |
  27. | 6 | jingliyang | female | 18 | 204 | 202 | 销售 |
  28. | 6 | jingliyang | female | 18 | 204 | 203 | 运营 |
  29. +----+------------+--------+------+--------+------+--------------+

交叉连接

2.内连接:只连接匹配的行

  1. #找两张表共有的部分,相当于利用条件从笛卡尔积结果中筛选出了正确的结果
  2. #department没有204这个部门,因而employee表中关于204这条员工信息没有匹配出来
  3. mysql> select employee.id,employee.name,employee.age,employee.sex,department.name from employee inner join department on employee.dep_id=department.id;
  4. +----+-----------+------+--------+--------------+
  5. | id | name | age | sex | name |
  6. +----+-----------+------+--------+--------------+
  7. | 1 | egon | 18 | male | 技术 |
  8. | 2 | alex | 48 | female | 人力资源 |
  9. | 3 | wupeiqi | 38 | male | 人力资源 |
  10. | 4 | yuanhao | 28 | female | 销售 |
  11. | 5 | liwenzhou | 18 | male | 技术 |
  12. +----+-----------+------+--------+--------------+
  13.  
  14. #上述sql等同于
  15. mysql> select employee.id,employee.name,employee.age,employee.sex,department.name from employee,department where employee.dep_id=department.id;

内连接

3.外连接之左右连接

  1. #优先显示左表全部记录
  2. #以左表为准,即找出所有员工信息,当然包括没有部门的员工
  3. #本质就是:在内连接的基础上增加左边有右边没有的结果
  4. mysql> select employee.id,employee.name,department.name as depart_name from employee left join department on employee.dep_id=department.id;
  5. +----+------------+--------------+
  6. | id | name | depart_name |
  7. +----+------------+--------------+
  8. | 1 | egon | 技术 |
  9. | 5 | liwenzhou | 技术 |
  10. | 2 | alex | 人力资源 |
  11. | 3 | wupeiqi | 人力资源 |
  12. | 4 | yuanhao | 销售 |
  13. | 6 | jingliyang | NULL |
  14. +----+------------+--------------+
  15. ---------------------------------------------------------------------------
  16. #优先显示右表全部记录
  17. #以右表为准,即找出所有部门信息,包括没有员工的部门
  18. #本质就是:在内连接的基础上增加右边有左边没有的结果
  19. mysql> select employee.id,employee.name,department.name as depart_name from employee right join department on employee.dep_id=department.id;
  20. +------+-----------+--------------+
  21. | id | name | depart_name |
  22. +------+-----------+--------------+
  23. | 1 | egon | 技术 |
  24. | 2 | alex | 人力资源 |
  25. | 3 | wupeiqi | 人力资源 |
  26. | 4 | yuanhao | 销售 |
  27. | 5 | liwenzhou | 技术 |
  28. | NULL | NULL | 运营 |
  29. +------+-----------+--------------+

外连接.左右

4.全外连接:显示左右两个表全部记录

  1. 全外连接:在内连接的基础上增加左边有右边没有的和右边有左边没有的结果
  2. #注意:mysql不支持全外连接 full JOIN
  3. #强调:mysql可以使用此种方式间接实现全外连接
  4. select * from employee left join department on employee.dep_id = department.id
  5. union
  6. select * from employee right join department on employee.dep_id = department.id
  7. ;
  8. #查看结果
  9. +------+------------+--------+------+--------+------+--------------+
  10. | id | name | sex | age | dep_id | id | name |
  11. +------+------------+--------+------+--------+------+--------------+
  12. | 1 | egon | male | 18 | 200 | 200 | 技术 |
  13. | 5 | liwenzhou | male | 18 | 200 | 200 | 技术 |
  14. | 2 | alex | female | 48 | 201 | 201 | 人力资源 |
  15. | 3 | wupeiqi | male | 38 | 201 | 201 | 人力资源 |
  16. | 4 | yuanhao | female | 28 | 202 | 202 | 销售 |
  17. | 6 | jingliyang | female | 18 | 204 | NULL | NULL |
  18. | NULL | NULL | NULL | NULL | NULL | 203 | 运营 |
  19. +------+------------+--------+------+--------+------+--------------+
  20.  
  21. #注意 union与union all的区别:union会去掉相同的纪录

全外连接

                                                               符合条件连接查询                                              

  1. #示例1:以内连接的方式查询employee和department表,并且employee表中的age字段值必须大于25,即找出年龄大于25岁的员工以及员工所在的部门
  2. select employee.name,department.name from employee inner join department
  3. on employee.dep_id = department.id
  4. where age > 25;
  5.  
  6. #示例2:以内连接的方式查询employee和department表,并且以age字段的升序方式显示
  7. select employee.id,employee.name,employee.age,department.name from employee,department
  8. where employee.dep_id = department.id
  9. and age > 25
  10. order by age asc;

                                                                       子查询                                                         

  1. #1:子查询是将一个查询语句嵌套在另一个查询语句中。
  2. #2:内层查询语句的查询结果,可以为外层查询语句提供查询条件。
  3. #3:子查询中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等关键字
  4. #4:还可以包含比较运算符:= 、 !=、> 、<等

1.带IN关键字的子查询

  1. #查询平均年龄在25岁以上的部门名
  2. select id,name from department
  3. where id in
  4. (select dep_id from employee group by dep_id having avg(age) > 25);
  5.  
  6. #查看技术部员工姓名
  7. select name from employee
  8. where dep_id in
  9. (select id from department where name='技术');
  10.  
  11. #查看不足1人的部门名(子查询得到的是有人的部门id)
  12. select name from department where id not in (select distinct dep_id from employee);

2.带比较运算符的子查询

  1. #比较运算符:=、!=、>、>=、<、<=、<>
  2. #查询大于所有人平均年龄的员工名与年龄
  3. mysql> select name,age from emp where age > (select avg(age) from emp);
  4. +---------+------+
  5. | name | age |
  6. +---------+------+
  7. | alex | 48 |
  8. | wupeiqi | 38 |
  9. +---------+------+
  10. 2 rows in set (0.00 sec)
  11.  
  12. #查询大于部门内平均年龄的员工名、年龄
  13. select t1.name,t1.age from emp t1
  14. inner join
  15. (select dep_id,avg(age) avg_age from emp group by dep_id) t2
  16. on t1.dep_id = t2.dep_id
  17. where t1.age > t2.avg_age;

3.带EXISTS关键字的子查询

  1. EXISTS关字键字表示存在。在使用EXISTS关键字时,内层查询语句不返回查询的记录。
  2. 而是返回一个真假值。TrueFalse
  3. 当返回True时,外层查询语句将进行查询;当返回值为False时,外层查询语句不进行查询
  4.  
  5. #department表中存在dept_id=203,Ture
  6. mysql> select * from employee
  7. -> where exists
  8. -> (select id from department where id=200);
  9. +----+------------+--------+------+--------+
  10. | id | name | sex | age | dep_id |
  11. +----+------------+--------+------+--------+
  12. | 1 | egon | male | 18 | 200 |
  13. | 2 | alex | female | 48 | 201 |
  14. | 3 | wupeiqi | male | 38 | 201 |
  15. | 4 | yuanhao | female | 28 | 202 |
  16. | 5 | liwenzhou | male | 18 | 200 |
  17. | 6 | jingliyang | female | 18 | 204 |
  18. +----+------------+--------+------+--------+
  19.  
  20. #department表中存在dept_id=205,False
  21. mysql> select * from employee
  22. -> where exists
  23. -> (select id from department where id=204);
  24. Empty set (0.00 sec)

mysql数据库之多表查询的更多相关文章

  1. MySQL数据库语法-多表查询练习一

    MySQL数据库语法-多表查询练习一 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客主要介绍的多表查询的外键约束,以及如何使用外链接和内连接查询数据信息. 一.数据表和测试 ...

  2. MySQL数据库之单表查询中关键字的执行顺序

    目录 MySQL数据库之单表查询中关键字的执行顺序 1 语法顺序 2 执行顺序 3 关键字使用语法 MySQL数据库之单表查询中关键字的执行顺序 1 语法顺序 select distinct from ...

  3. MySQL数据库语法-单表查询练习

    MySQL数据库语法-单表查询练习 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客主要是对聚合函数和分组的练习. 一.数据表和测试数据准备 /* @author :yinz ...

  4. mysql数据库之单表查询多表查询

    单表查询 前期表准备 create table emp( id int not null unique auto_increment, name varchar(20) not null, sex e ...

  5. mysql数据库之单表查询

    单标查询 单表查询语句 关键字执行的优先级 简单查询 where约束 group by 聚合函数 HAVING过滤 order by 查询排序 LIMIT限制查询的记录数 使用正则表达式查询 单表查询 ...

  6. mysql数据库之联表查询

    表准备: 这次我们用到5张表: class表: student表: score表: course表: teacher表: 表结构模型: 我们针对以下需求分析联表查询: 1.查询所有的课程的名称以及对应 ...

  7. nodejs MYSQL数据库执行多表查询

    1.设计数据库 2.设计数据库表 genres表: books表: 3.安装MySQL模块 4. 代码编写 (1) 第一种方法: 在query中使用nextTables属性,将属性值设置为ture d ...

  8. 关于Mysql数据库进行多表查询时设计编程思想

    SQL代码:

  9. 4.mysql数据库创建,表中创建模具模板脚本,mysql_SQL99标准连接查询(恩,外部连接,全外连接,交叉连接)

     mysql数据库创建,表创建模等模板脚本 -- 用root用户登录系统,运行脚本 -- 创建数据库 create database mydb61 character set utf8 ; -- ...

随机推荐

  1. python基础--魔法方法、迭代器、上下文管理

    isinstance:判断一个对象是否是某个类的实例 参数一:要判断的对象 参数二:要判断的类型 issubclass:判断一个类是否是另一个类的子类 参数一:是待判断的子类 参数二:待判断的父类 _ ...

  2. innerhtml outhtml innerText outText 区别

    innerHTML获取标签内的HTML outerHTML获取标签及标签内的HTML innerText 设置或获取位于对象起始和结束标签内的文本 outerText 设置(包括标签)或获取(不包括标 ...

  3. Leetcode46. Permutations全排列

    给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1 ...

  4. 两张图搞清composer install与update区别 - 今日头条(www.toutiao.com)

    composer update : 主要是在开发阶段使用,根据我们在composer.json文件中指定的内容升级项目的依赖包. composer install : 主要是在部署阶段使用,以便在生产 ...

  5. 2019阿里云开年Hi购季域名与商标分会场全攻略!

    2019阿里云云上Hi购季活动已经于2月25日正式开启,从已开放的活动页面来看,活动分为三个阶段: 2月25日-3月04日的活动报名阶段.3月04日-3月16日的新购满返+5折抢购阶段.3月16日-3 ...

  6. 【python之路20】函数作为参数

    1.函数可以作为参数 1)函数名相当于变量指向函数 2)函数名后面加括号表示调用函数 #!usr/bin/env python # -*- coding:utf-8 -*- def f1(args): ...

  7. c中函数指针和回调函数

    函数指针: 指向函数的指针.(定义的函数会分配一块内存,同变量一样存在首地址)示例如下: int Func(int x); /*声明一个函数*/ int (*p) (int x); /*定义一个函数指 ...

  8. oracle-12333错误

    Errors in file /oracle/OraHome1/admin/hndw/udump/hndw_ora_17941.trc: ORA-00600: internal error code, ...

  9. Leetcode728.Self Dividing Numbers自除数

    自除数 是指可以被它包含的每一位数除尽的数. 例如,128 是一个自除数,因为 128 % 1 == 0,128 % 2 == 0,128 % 8 == 0. 还有,自除数不允许包含 0 . 给定上边 ...

  10. 【风马一族_php】常用的语句

    设置脚本的编码 <?php header('Content-type:text/html;charset=utf-8');  ?> 按原格式的输入内容 echo <pre>; ...