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

本文介绍了如何在MySQL中进行内连接查询,包括隐式和显示内连接,以及查询特定部门的员工。接着讨论了外连接,包括左外连接、右外连接和满外连接,展示了如何查找有无员工的部门。此外,还详细讲解了子查询的四种类型,并举例说明了如何使用ALL、ANY、SOME和IN关键字。最后,文章提到了自关联查询的应用,展示了如何在一个表中查询上下级关系的数据。
摘要由CSDN通过智能技术生成

1. 内连接查询

数据准备:

use mydb3;

-- 创建部门表
create table if not exists dept3(
  deptno varchar(20) primary key ,  -- 部门号
  name varchar(20) -- 部门名字
);
 
-- 创建员工表
create table if not exists emp3(
  eid varchar(20) primary key , -- 员工编号
  ename varchar(20), -- 员工名字
  age int,  -- 员工年龄
  dept_id varchar(20)  -- 员工所属部门
);

-- 给dept3表添加数据
insert into dept3 values('1001','研发部');
insert into dept3 values('1002','销售部');
insert into dept3 values('1003','财务部');
insert into dept3 values('1004','人事部');

-- 给emp表添加数据
insert into emp3 values('1','乔峰',20, '1001');
insert into emp3 values('2','段誉',21, '1001');
insert into emp3 values('3','虚竹',23, '1001');
insert into emp3 values('4','阿紫',18, '1001');
insert into emp3 values('5','扫地僧',85, '1002');
insert into emp3 values('6','李秋水',33, '1002');
insert into emp3 values('7','鸠摩智',50, '1002'); 
insert into emp3 values('8','天山童姥',60, '1003');
insert into emp3 values('9','慕容博',58, '1003');
insert into emp3 values('10','丁春秋',71, '1005');

内连接查询语法:

-- 语法:
-- 隐式内连接(SQL92标准):select * from A,B where 条件; 
-- 显示内连接(SQL99标准):select * from A inner join B on 条件;

-- 查询每个部门的所属员工
select * from dept3,emp3 where dept3.deptno = emp3.dept_id;
select * from dept3 inner join emp3 on dept3.deptno = emp3.dept_id;
-- 查询研发部和销售部的所属员工
select * from dept3,emp3 where dept3.deptno = emp3.dept_id and name in( '研发部','销售部');
select * from dept3 join emp3 on dept3.deptno = emp3.dept_id and name in( '研发部','销售部');
 
-- 查询每个部门的员工数,并升序排序
select deptno,count(1) as total_cnt from dept3,emp3 where dept3.deptno = emp3.dept_id group by deptno order by total_cnt;
 
select deptno,count(1) as total_cnt from dept3 join emp3 on dept3.deptno = emp3.dept_id group by deptno order by total_cnt;
-- 查询研发部和销售部的所属员工
select * from dept3,emp3 where dept3.deptno = emp3.dept_id and name in( '研发部','销售部');
select * from dept3 join emp3 on dept3.deptno = emp3.dept_id and name in( '研发部','销售部');
 
-- 查询每个部门的员工数,并升序排序
select deptno,count(1) as total_cnt from dept3,emp3 where dept3.deptno = emp3.dept_id group by deptno order by total_cnt;
 
select deptno,count(1) as total_cnt from dept3 join emp3 on dept3.deptno = emp3.dept_id group by deptno order by total_cnt;

-- 查询人数大于等于3的部门,并按照人数降序排序
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;
 
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 条件;

-- 外连接查询
-- 查询哪些部门有员工,哪些部门没有员工
use mydb3;
select * from dept3 left outer join emp3 on dept3.deptno = emp3.dept_id;
 
-- 查询哪些员工有对应的部门,哪些没有
select * from dept3 right outer join emp3 on dept3.deptno = emp3.dept_id;
 
 
-- 使用union关键字实现左外连接和右外连接的并集
select * from dept3 left outer join emp3 on dept3.deptno = emp3.dept_id
union 
select * from dept3 right outer join emp3 on dept3.deptno = emp3.dept_id;

3. 子查询

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

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

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

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

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

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

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

ALL关键字:

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

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

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

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

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

-- 语法:
-- select …from …where c > all(查询语句)
-- 等价于:
-- select ...from ... where c > result1 and c > result2 and c > result3

-- 查询年龄大于‘1003’部门所有年龄的员工信息
select * from emp3 where age > all(select age from emp3 where dept_id = '1003’);

-- 查询不属于任何一个部门的员工信息 
select * from emp3 where dept_id != all(select deptno from dept3); 

ANY与SOME关键字:

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

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

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

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

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

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

-- 语法:
-- select …from …where c > any(查询语句)
-- 等价于:
-- select ...from ... where c > result1 or c > result2 or c > result3

-- 查询年龄大于‘1003’部门任意一个员工年龄的员工信息
select * from emp3 where age > all(select age from emp3 where dept_id = '1003’);

IN关键字:

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

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

-- 语法:
-- select …from …where c in(查询语句)
-- 等价于:
-- select ...from ... where c = result1 or c = result2 or c = result3

-- 查询研发部和销售部的员工信息,包含员工号、员工名字
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关键字。

-- 语法:
-- select …from …where exists(查询语句)

-- 查询公司是否有大于60岁的员工,有则输出
select * from emp3 a where exists(select * from emp3 b where a.age > 60);
 
-- 查询有所属部门的员工信息
select * from emp3 a where exists(select * from dept3 b where a.dept_id = b.deptno);

4. 自关联查询

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

成多张表来用。

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

-- 语法:
-- select 字段列表 from 表1 a , 表1 b where 条件;
-- 或者 
-- select 字段列表 from 表1 a [left] join 表1 b on 条件;

-- 创建表,并建立自关联约束
create table t_sanguo(
    eid int primary key ,
    ename varchar(20),
    manager_id int,
 foreign key (manager_id) references t_sanguo (eid)  -- 添加自关联约束
);

-- 添加数据 
insert into t_sanguo values(1,'刘协',NULL);
insert into t_sanguo values(2,'刘备',1);
insert into t_sanguo values(3,'关羽',2);
insert into t_sanguo values(4,'张飞',2);
insert into t_sanguo values(5,'曹操',1);
insert into t_sanguo values(6,'许褚',5);
insert into t_sanguo values(7,'典韦',5);
insert into t_sanguo values(8,'孙权',1);
insert into t_sanguo values(9,'周瑜',8);
insert into t_sanguo values(10,'鲁肃',8);
 
-- 进行关联查询
-- 1.查询每个三国人物及他的上级信息,如:  关羽  刘备 
select * from t_sanguo a, t_sanguo b where a.manager_id = b.eid;

(日常美图时间)

 

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三月七꧁ ꧂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值