• MySQL---多表联合查询(下)(内连接查询、外连接查询、子查询(ALL/ANY/SOME/IN/EXISTS关键字)、自关联查询)


    1. 内连接查询

    数据准备:

    1. use mydb3;
    2. -- 创建部门表
    3. create table if not exists dept3(
    4. deptno varchar(20) primary key , -- 部门号
    5. name varchar(20) -- 部门名字
    6. );
    7.  
    8. -- 创建员工表
    9. create table if not exists emp3(
    10. eid varchar(20) primary key , -- 员工编号
    11. ename varchar(20), -- 员工名字
    12. age int, -- 员工年龄
    13. dept_id varchar(20) -- 员工所属部门
    14. );
    15. -- 给dept3表添加数据
    16. insert into dept3 values('1001','研发部');
    17. insert into dept3 values('1002','销售部');
    18. insert into dept3 values('1003','财务部');
    19. insert into dept3 values('1004','人事部');
    20. -- 给emp表添加数据
    21. insert into emp3 values('1','乔峰',20, '1001');
    22. insert into emp3 values('2','段誉',21, '1001');
    23. insert into emp3 values('3','虚竹',23, '1001');
    24. insert into emp3 values('4','阿紫',18, '1001');
    25. insert into emp3 values('5','扫地僧',85, '1002');
    26. insert into emp3 values('6','李秋水',33, '1002');
    27. insert into emp3 values('7','鸠摩智',50, '1002');
    28. insert into emp3 values('8','天山童姥',60, '1003');
    29. insert into emp3 values('9','慕容博',58, '1003');
    30. insert into emp3 values('10','丁春秋',71, '1005');

    内连接查询语法:

    1. -- 语法:
    2. -- 隐式内连接(SQL92标准):select * from A,B where 条件;
    3. -- 显示内连接(SQL99标准):select * from A inner join B on 条件;
    4. -- 查询每个部门的所属员工
    5. select * from dept3,emp3 where dept3.deptno = emp3.dept_id;
    6. select * from dept3 inner join emp3 on dept3.deptno = emp3.dept_id;
    1. -- 查询研发部和销售部的所属员工
    2. select * from dept3,emp3 where dept3.deptno = emp3.dept_id and name in( '研发部','销售部');
    3. select * from dept3 join emp3 on dept3.deptno = emp3.dept_id and name in( '研发部','销售部');
    4.  
    5. -- 查询每个部门的员工数,并升序排序
    6. select deptno,count(1) as total_cnt from dept3,emp3 where dept3.deptno = emp3.dept_id group by deptno order by total_cnt;
    7.  
    8. select deptno,count(1) as total_cnt from dept3 join emp3 on dept3.deptno = emp3.dept_id group by deptno order by total_cnt;
    1. -- 查询研发部和销售部的所属员工
    2. select * from dept3,emp3 where dept3.deptno = emp3.dept_id and name in( '研发部','销售部');
    3. select * from dept3 join emp3 on dept3.deptno = emp3.dept_id and name in( '研发部','销售部');
    4.  
    5. -- 查询每个部门的员工数,并升序排序
    6. select deptno,count(1) as total_cnt from dept3,emp3 where dept3.deptno = emp3.dept_id group by deptno order by total_cnt;
    7.  
    8. select deptno,count(1) as total_cnt from dept3 join emp3 on dept3.deptno = emp3.dept_id group by deptno order by total_cnt;
    1. -- 查询人数大于等于3的部门,并按照人数降序排序
    2. select deptno,count(1) as total_cnt from dept3,emp3 where dept3.deptno = emp3.dept_id group by deptno having total_cnt >= 3 order by total_cnt desc;
    3.  
    4. select deptno,count(1) as total_cnt from dept3 join emp3 on dept3.deptno = emp3.dept_id group by deptno having total_cnt >= 3 order by total_cnt desc;

    2. 外连接查询

    语法: 左外连接:left outer join:

                select * from A left outer join B on 条件;

                右外连接:right outer join:

                select * from A right outer join B on 条件;

                满外连接: full outer join:

                select * from A full outer join B on 条件;

    1. -- 外连接查询
    2. -- 查询哪些部门有员工,哪些部门没有员工
    3. use mydb3;
    4. select * from dept3 left outer join emp3 on dept3.deptno = emp3.dept_id;
    5.  
    6. -- 查询哪些员工有对应的部门,哪些没有
    7. select * from dept3 right outer join emp3 on dept3.deptno = emp3.dept_id;
    8.  
    9.  
    10. -- 使用union关键字实现左外连接和右外连接的并集
    11. select * from dept3 left outer join emp3 on dept3.deptno = emp3.dept_id
    12. union
    13. select * from dept3 right outer join emp3 on dept3.deptno = emp3.dept_id;

    3. 子查询

    子查询就是指的在一个完整的查询语句之中,嵌套若干个不同功能的小查询。通俗一点就是包含select嵌套的查询。

    子查询可以返回的数据类型一共分为四种:

    单行单列:返回的是一个具体列的内容,可以理解为一个单值数据;

    单行多列:返回一行数据中多个列的内容;

    多行单列:返回多行记录之中同一列的内容,相当于给出了一个操作范围;

    多行多列:查询返回的结果是一张临时表。

    1. -- 查询年龄最大的员工信息,显示信息包含员工号、员工名字,员工年龄
    2. select eid,ename,age from emp3 where age = (select max(age) from emp3);
    3.  
    4.  
    5. -- 查询年研发部和销售部的员工信息,包含员工号、员工名字
    6. select eid,ename,t.name from emp3 where dept_id in (select deptno,name from dept3 where name = '研发部' or name = '销售部') ;
    7.  
    8.  
    9. -- 查询研发部20岁以下的员工信息,包括员工号、员工名字,部门名字
    10. select eid,age,ename,name from (select * from dept where name = '研发部 ')t1,(select * from emp3 where age <20)t2

    ALL关键字:

    与子查询返回的所有值比较为true 则返回true。

    ALL可以与=>>=<<=<>结合是来使用,分别表示等于、大于、大于等于、小于、小于

    于、不等于其中的所有数据。

    ALL表示指定列中的值必须要大于子查询集的每一个值,即必须要大于子查询集的最大值;如果是

    小于号即小于子查询集的最小值。同理可以推出其它的比较运算符的情况

    1. -- 语法:
    2. -- select …from …where c > all(查询语句)
    3. -- 等价于:
    4. -- select ...from ... where c > result1 and c > result2 and c > result3
    5. -- 查询年龄大于‘1003’部门所有年龄的员工信息
    6. select * from emp3 where age > all(select age from emp3 where dept_id = '1003’);
    7. -- 查询不属于任何一个部门的员工信息
    8. select * from emp3 where dept_id != all(select deptno from dept3);

    ANY与SOME关键字:

    与子查询返回的任何值比较为true,则返回true。

    ANY可以与=、>、>=、<、<=、<>结合是来使用,分别表示等于、大于、大于等于、小于、小于

    等于、不等于其中的任何一个数据。

    表示指定列中的值要大于子查询中的任意一个值,即必须要大于子查询集中的最小值。同理可以推

    出其它的比较运算符的情况。

    SOME和ANY的作用一样,SOME可以理解为ANY的别名。

    1. -- 语法:
    2. -- select …from …where c > any(查询语句)
    3. -- 等价于:
    4. -- select ...from ... where c > result1 or c > result2 or c > result3
    5. -- 查询年龄大于‘1003’部门任意一个员工年龄的员工信息
    6. select * from emp3 where age > all(select age from emp3 where dept_id = '1003’);

    IN关键字:

    IN关键字,用于判断某个记录的值,是否在指定的集合中。

    在IN关键字前边加上not可以将条件反过来。

    1. -- 语法:
    2. -- select …from …where c in(查询语句)
    3. -- 等价于:
    4. -- select ...from ... where c = result1 or c = result2 or c = result3
    5. -- 查询研发部和销售部的员工信息,包含员工号、员工名字
    6. select eid,ename,t.name from emp3 where dept_id in (select deptno from dept3 where name = '研发部' or name = '销售部') ;

    EXISTS关键字:

    该子查询如果“有数据结果”(至少返回一行数据), 则该EXISTS() 的结果为“true”,外层查询执行

    子查询如果“没有数据结果”(没有任何数据返回),则该EXISTS()的结果为“false”,外层查询

    不执行。

    EXISTS后面的子查询不返回任何实际数据,只返回真或假,当返回真时 where条件成立。

    EXISTS关键字,比IN关键字的运算效率高,因此,在实际开发中,特别是大数据量时,推

    荐使用EXISTS关键字。

    1. -- 语法:
    2. -- select …from …where exists(查询语句)
    3. -- 查询公司是否有大于60岁的员工,有则输出
    4. select * from emp3 a where exists(select * from emp3 b where a.age > 60);
    5.  
    6. -- 查询有所属部门的员工信息
    7. select * from emp3 a where exists(select * from dept3 b where a.dept_id = b.deptno);

    4. 自关联查询

    MySQL有时在信息查询时需要进行对表自身进行关联查询,即一张表自己和自己关联,一张表当

    成多张表来用。

    注意自关联时表必须给表起别名。

    1. -- 语法:
    2. -- select 字段列表 from 表1 a , 表1 b where 条件;
    3. -- 或者
    4. -- select 字段列表 from 表1 a [left] join 表1 b on 条件;
    5. -- 创建表,并建立自关联约束
    6. create table t_sanguo(
    7. eid int primary key ,
    8. ename varchar(20),
    9. manager_id int,
    10. foreign key (manager_id) references t_sanguo (eid) -- 添加自关联约束
    11. );
    12. -- 添加数据
    13. insert into t_sanguo values(1,'刘协',NULL);
    14. insert into t_sanguo values(2,'刘备',1);
    15. insert into t_sanguo values(3,'关羽',2);
    16. insert into t_sanguo values(4,'张飞',2);
    17. insert into t_sanguo values(5,'曹操',1);
    18. insert into t_sanguo values(6,'许褚',5);
    19. insert into t_sanguo values(7,'典韦',5);
    20. insert into t_sanguo values(8,'孙权',1);
    21. insert into t_sanguo values(9,'周瑜',8);
    22. insert into t_sanguo values(10,'鲁肃',8);
    23.  
    24. -- 进行关联查询
    25. -- 1.查询每个三国人物及他的上级信息,如: 关羽 刘备
    26. select * from t_sanguo a, t_sanguo b where a.manager_id = b.eid;

    (日常美图时间)

     

  • 相关阅读:
    C语言switch语句判断星期
    26. Python数据类型之列表
    git修改提交历史中的author信息
    冰蝎默认加密的流量解密
    Visio 安装与操作小结
    报错!Jupyter notebook 500 : Internal Server Error
    【网络工程】3、交换机原理
    计算机毕业设计Java在线选课系统设计(系统+程序+mysql数据库+Lw文档)
    抑制剂&拮抗剂等小分子化合物
    Dobot机械臂的Python Demo
  • 原文地址:https://blog.csdn.net/weixin_43961909/article/details/130624954