4.子查询

子查询: 在insert update delete select 中 from 后再嵌套一个selec语句x,
这个x就称为子查询。
子查询的selec语句必须放在小括号中。


例: 查询Apple的系主任姓名 t_student t_dept t_man
select did from t_student where sename='Apple';
select mid from t_dept where did = ***;
select mname from t_man where mid= ***;

select manme from t_man wher mid = (select mid from t_dept where did =
(select did from t_student where sename='Apple'));

例:统计 与 张凡 在一个班的学生有多少
select scalss from t_student where sname='张凡';
select count(*) from t_student where sclass =
( select scalss from t_student where sname='张凡');
例: 查询表中哪些人的分数平均分以上
select * from t_student where sscore > (select avg(sscore) from t_student );

例: 统计每个班有多少人是平均年龄以上。

select sclass,count(*) from t_student
where sage > (select avg(sage) from t_student)
group by sclass;

例: 将学生的学号、姓名、英文名放入另一张表中 x(a,b,c)
insert into x(a,b,c) select sid,sname,senam from t_student;


select left(sname,1) from (select sname from (select * from t_student where ssex='男') as a) as b;

// 按照系编号分组,查询最大的的平均年龄,
select max(x) from ( select avg(sage) as x from t_student group by did ) as a;

// 按照系编号分组,查询最大的的平均年龄系的信息,
select did from (select did,avg(sage) as x from t_student group by did) as t2
where x =
(select max(x) from (select did,avg(sage)
as x from t_student group by did) as t );


select * from t_dept where did = ();





// 按照系编号分组,查询最大的的平均年龄系的信息,

1、查询每个系的系编号和系的平均年龄,结果是t1表
select did,avg(sage) as x from t_student group by did
2、去t1表中查询最大的x记录为 n
select max(x) from (select did,avg(sage) as x from t_student group by did) as t1
3、再根据最大的x记录n去t1表中查询出对应的系编号
select did from (select did,avg(sage) as x from t_student group by did) as t1
where x = (select max(x) from (select did,avg(sage) as x from t_student group by did) as t2 );
4、通过第3步查询编号去系部表中查询这个系的信息l
select * from t_dept where did =
( select did from
(select did,avg(sage) as x from t_student group by did) as t1
where x = (
select max(x) from (
select did,avg(sage) as x from t_student group by did) as t2 ));



//查询出每个系里面有多少学生是他的系平均分以上
select * from t_student where sscore > (select avg(sscore) from t_student);
select did,count(*) from t_student
where sscore > (select avg(sscore) from t_student)
group by did;

all any in
all:是指所有
any:是指任意一个
in:只要等于一个

例: 查询表中有哪些学生 比3班某一个学生都大的学生信息
select * from t_student where sage > any (select sage from t_student where sclass=3)

select * from t_student sclass in (select sclass from t_student where did = 1)

查询年龄与Tea或Ice或Eat年龄相同的学生的姓名。
select sname from t_student where sage in (
select sage from t_student where sename in ('Tea','Ice','Eat')
);

找出比考试分数500以上每个学生年龄都大的学生的姓名。
select sname from t_student where sage > all
(select sage from t_student where sscore > 500);


例:求每个系的系编号,系名字,及系的人数。
select did,dname,(select count(*) from t_student
where t_student.did=t_dept.did) from t_dept;

找出每个系的系编号和系名字及系主任的名字
select did ,dname,(select mname from t_man where
t_dept.mid= t_man.mid
) from t_dept;


select 姓名 from t;
select 分数 as '语文' from t where 科目 = '语文';
select 分数 as '数学' from t where 科目 = '数学';
select 分数 as '英语' from t where 科目 = '英语';
select 姓名 from t group by 姓名;

select 姓名,
(select 分数 from t as t1 where 科目='语文' and t1.姓名=t2.姓名) as 语文,
(select 分数 from t as t1 where 科目='数学' and t1.姓名=t2.姓名) as 数学,
(select 分数 from t as t1 where 科目='英语' and t1.姓名=t2.姓名) as 英语
from t as t2 group by 姓名;

猜你喜欢

转载自www.cnblogs.com/makalochen/p/10656315.html