1.in --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 和 some 同义词 --any后面只能跟子查询语句
# 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
# 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);
Example: data preparation
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);
1、查询选修了所有课程的学生id、name:(即该学生根本就不存在一门他没有选的课程。
方法1:选了3门课程的学生其实就是选了所有的
select s.id,s.name from student as s inner join student2course as sc on s.id = sc.sid
group by sc.id having count(sc.id)=(select count(id) from course);
方法2:找到这样的学生,该学生不存在没选的课程
select * from student as s where not exists (
select * from course as c where not exists (
select * from student2course as sc where s.id=sc.sid and c.id=sc.cid));
2、查询没有选择所有课程的学生,即没有全选的学生。(存在这样的一个学生,他至少有一门课没有选)
select * from student as s where exists (
select * from course as c where not exists (
select * from student2course as sc where s.id=sc.sid and c.id=sc.cid));
3、查询一门课也没有选的学生。(不存这样的一个学生,他至少选修一门课程)
select * from student as s where not exists (
select * from student2course as sc where s.id=sc.sid);
# 4、查询至少选修了一门课程的学生。
select * from student as s where exists (
select * from student2course as sc where s.id=sc.sid);