– 知道一个学生的名字,张三,想知道他在哪个班级
– 1,通过学生表获取他的班级ID
– 2, 通过班级ID获取班级名字
– 标量子查询
select * from class where class_id = (select class_id from my_student where stu_name = ‘张三’);
– 想获取在班学生的所有班级名字
– 1,找出学生表中所有班级ID
– 2, 根据班级ID找出所有班级名字
– 列子查询
select name from class where class_id in (select class_id from my_student);
– 主查询 where 条件[(构造一个行元素)] = (行子查询);
select * from my_student where (stu_age,stu_height) = (select max(stu_age),min(stu_height) from my_student);
标量子查询、列子查询和行子查询:都属于where子查询
– 获取每班最高学生
– 1,把最高学生放在第一位 order by
– 2,在对结果进行group by ,保留每组第一个
– 表子查询
select * from (select * from my_student order by stu_height desc) as temp group by class_id;
– exists 查询,求出有学生所在班级
select * from class as c where exists(select stu_id from my_student as s where s.class_id = c.class_id);