联合查询
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,idfrom my_student;
-- 需求:男生升序,女生降序(年龄)(
select * from my_studentwhere sex='男' order by age asc limit 9999999)
union(
select *from my_studentwhere sex='女' order by agedesc limit 9999999);
标量子查询
select * from my_studentwhere c_id=(select id from my_classwhere name='python1910');
列子查询
select * from my_studentwhere c_idin (select id from my_class);
-- any some all
select * from my_studentwhere c_id =any (select id from my_class);
select * from my_studentwhere c_id =some (select id from my_class);
select * from my_studentwhere c_id =all (select id from my_class);
select * from my_studentwhere c_id !=any (select id from my_class);-- 所有结果(null除外)
select * from my_studentwhere c_id !=some (select id from my_class);-- 所有结果(null除外)
select * from my_studentwhere c_id !=all (select id from my_class);-- 1,2不等于345(null除外)
select * from my_studentwhere age=(select max(age)from my_student)and height=(select max(height)from my_student);
行子查询
select * from my_studentwhere (age,height)= (select max(age),max(height)from my_student);
select *from my_studentorder by age desc,heightdesc limit 1;-- 可能查询结果不是预想的
表子查询
select * from my_studentgroup by c_idorder by heightdesc;
-- 插入学生
insert into my_studentvalues (null,'bc20200007','小红','女',25,185,1);
-- 查找每个班身高最高的学生
select * from (select * from my_studentorder by heightdesc limit 9999999)as studentgroup by c_id;
--
select exists(select * from my_student);
select exists(select * from my_studentwhere id=100);
exists子查询
select * from my_studentwhere exists(select * from my_class);-- 判断括号里是否成立
select * from my_studentwhere exists(select * from my_classwhere id=3);
select * from my_studentwhere exists(select * from my_classwhere id=2);