数据库之mysql多表查询(子查询)以及pymysql等相关内容-46

1.子查询in

# 1、in
select * from emp where age=18 or age=38 or age=28;
select * from emp where age in (18,38,28);
?
# 子查询的思路
select * from emp where dep_id in
(select id from dep where name="技术" or name="销售");
?
# 链表的思路
select * from emp inner join dep
on emp.dep_id = dep.id
where dep.name in ("技术","销售");
?
?
# not in不支持null
mysql> select * from dep;
+------+--------------+
| id   | name         |
+------+--------------+
|  200 | 技术         |
|  201 | 人力资源     |
|  202 | 销售         |
|  203 | 运营         |
+------+--------------+
4 rows in set (0.00 sec)
mysql> insert into emp values(7,‘lili‘,‘female‘,48,null);
Query OK, 1 row affected (0.03 sec)
?
mysql> select * from emp
  -> ;
+----+------------+--------+------+--------+
| id | name       | sex   | age | dep_id |
+----+------------+--------+------+--------+
|  1 | egon       | male   |   18 |    200 |
|  2 | alex       | female |   48 |    201 |
|  3 | wupeiqi   | male   |   38 |    201 |
|  4 | yuanhao   | female |   28 |    202 |
|  5 | liwenzhou | male   |   18 |    200 |
|  6 | jingliyang | female |   18 |    204 |
|  7 | lili       | female |   48 |   NULL |
+----+------------+--------+------+--------+
7 rows in set (0.00 sec)
?
mysql>
?
?
查询出有员工的部门,
select * from dep where id in
(select distinct dep_id from emp);
?
查询出没有员工的部门,
select * from dep where id not in
(select distinct dep_id from emp);
?
?
select * from dep where id not in
(select distinct dep_id from emp where dep_id is not null);

2.子查询any和all

# any后也跟子查询语句,与in不一样的地方在哪里
#   in (子查询语句)
#   in (值1,值2,值3)
#   而any只能跟子查询语句
#   any必须跟比较运算符配合使用
?
select * from emp where dep_id in
(select id from dep where name in ("技术","人力资源"));
?
select * from emp where dep_id = any
(select id from dep where name in ("技术","人力资源"));
?
?
select * from emp where dep_id not in
(select id from dep where name in ("技术","人力资源"));
?
select * from emp where ! (dep_id = any(select id from dep where name in ("技术","人力资源")));
?
?
查询出那些薪资比所有部门的平均薪资都高的员工=》薪资在所有部门平均线以上的狗币资本家
select * from employee where salary > all
(select avg(salary) from employee where depart_id is not null group by depart_id);
?
查询出那些薪资比所有部门的平均薪资都低的员工=》薪资在所有部门平均线以下的无产阶级劳苦大众
select * from employee where salary < all
(select avg(salary) from employee where depart_id is not null group by depart_id);
?
查询出那些薪资比任意一个部门的平均薪资高的员工=》薪资在任一部门平均线以上的员工
select * from employee where salary > any
(select avg(salary) from employee where depart_id is not null group by depart_id);
?
?
select * from employee where salary < any
(select avg(salary) from employee where depart_id is not null group by depart_id);

3.子查询exists

# 准备数据
create database db13;
?
use db13
?
create table student(
  id int primary key auto_increment,
  name varchar(16)
);
?
create table course(
  id int primary key auto_increment,
  name varchar(16),
   comment varchar(20)
);
?
create table student2course(
  id int primary key auto_increment,
  sid int,
  cid int,
   foreign key(sid) references student(id),
   foreign key(cid) references course(id)
);
?
?
insert into student(name) values
("egon"),
("lili"),
("jack"),
("tom");
?
insert into course(name,comment) values
("数据库","数据仓库"),
("数学","根本学不会"),
("英语","鸟语花香");
?
?
insert into student2course(sid,cid) values
(1,1),
(1,2),
(1,3),
(2,1),
(2,2),
(3,2);
?
准备数据
# exists  vs in
# in的效果 高于 exists
# 见博客:https://www.cnblogs.com/linhaifeng/articles/7267596.html#_label4
?
select * from 表1 where exists (select * from 表2);
?
# 例如:查询有员工的部门=》
select * from dep where exists (select * from emp where dep.id=emp.dep_id);
?
?
# not exists的效果 高于 not in
select * from dep where not exists (select * from emp where 203=emp.dep_id);
?
?
?
# 例:查询选修了所有课程的学生id、name:
?
# 实现方式一:选修了三门课程的学生就是选修了所有课程的学生
select s.id,s.name from student as s inner join student2course as sc
on s.id = sc.sid
group by sc.sid
having count(sc.cid) = (select count(id) from course);
?
# 实现方式二:找到这样的学生,该学生不存在没有选修过的课程
select * from student as s where not exists (
   select * from course as c where not exists (
       select * from student2course as sc where sc.sid = s.id and sc.cid = c.id
  )
);
?
?
select * from student as s where not exists (
   select * from course as c where not exists (
       select * from student2course as sc where sc.sid = s.id and sc.cid = c.id
  )
);
?
?
?
学生记录可以过滤出来,一定是子查询内没有记录
?
for 学生: # s.id=2
   for 课程: # c.id=1
       for 学生2课程: # sc.sid = 2 and sc.cid = 1
          pass
?
==================================
for sid in [1,2,3,4]:
   for cid in [1,2,3]:
      (sid,cid)
?
?
最外层循环一次
# (1,1)
# (1,2)
# (1,3)
最外层循环二次
# (2,1)
# (2,2)
# (2,3)
最外层循环三次
# (3,1)
# (3,2)
# (3,3)
最外层循环四次
# (4,1)
# (4,2)
# (4,3)
?
===================================
# 例2、查询没有选择所有课程的学生,即没有全选的学生。=》找出这样的学生,存在没有选修过的课程
select * from student as s where exists (
   select * from course as c where not exists (
       select * from student2course as sc where sc.sid = s.id and sc.cid = c.id
  )
);
?
# 例3、查询一门课也没有选的学生=》找出这样的学生,不存在选修过的课程
select * from student as s where
上一篇:用三种解决方案优化MySQL两千万数据大表


下一篇:JS 清空对象的值