联合查询
基本语法:
select 语句1
union [union 选项]
select 语句2……
union选项:()
all:保留所有,不管重复
distinct:去重,默认
--同一张表
--联合查询多张表查询 去重
select * from my_class
union
select * from my_class;
--不去重
select * from my_class
union all --不去重
select * from my_class;
--不相同的表查询的内容也不相同
select id,c_name,room from my_class
union all --不去重
select name,number,id from my_student;
下面我们做一个小练习
--男生升序,女生降序
(select * from my_student where sex='男' order by age asc limit 99999999)
union
(select * from my_student where sex='女' order by age desc limit 99999999)
按位置分类
from子查询
where子查询
exists 子查询
按结果分类
标量子查询
--一行一列
select * from my_student where c_id =(select id from my_class where c_name="python1809");
l列子查询
select * from my_student where c_id
in--多个数据源
(select id from my_class);
行子查询
select * from my_student where age =(elect max(age) from my_student)
ang
height =(select max(age) ,max(heiht) from my_student);
select * from my_student where (age,height) = (selet max (max),selet max (height)
from my_student);
select * from my_student order by age besc,height desc limit 1;
表子查询
select * from my_student where
exists (select * from my_class where id=1);
select * from my_student where
exists (select * from my_class where id=2);