子查询:
将查询语句作为外部sql的条件
注意
1子查询不能使用order by
2多行子查询可以返回多行记录,外部要使用多行操作符(In,exists,any ,all… )
where 或者 having +in ,not in,>,<,<=等等+跟子查询
,from后面+跟子查询
where sno>(select …)
having >(select …)
内联视图:
selec * from A, (select …from B) bb…:A表和查询的B表内连接
举例:
查询大于学生平均年龄的学号和姓名
where sno>(select …)
having >(select …)
内联视图:
selec * from A, (select …from B) bb…:A表和查询的B表内连接
in
exists/not exists返回真假值,不返回数据,性能会比in来的好
如果有exists后面是真 相当where 1=1,否则where 1=2
all比较子查询返回列表中的每一个值
> all :大于最大值
< all :小于最小值
=all:对比的是All后面的集合,几个有多个数据不返回结果,有一个数据=all相当于=数据
< all :小于最小值
=all:等于all集合的所有值
any适用于获取扣除最大和最小的数据的集合
\< any :小于最大的
> any :大于最小的
=any:等价于in
\< any :小于最大的
=any:
多列子查询
成对比较:多个列的数据要同时匹配
where (name ,id)in (select name ,id …..)
非成对比较:通过指定关键字and 或or ,and 关键字,指定多个列的数据要同时匹配;
or 关键字,指定多个列的数据不必同时匹配;
非成对比较:就是查询将条件分开,不是成对出现
where name in (select name …..) and id in (select id …..)
select no,name from a
where (no,name) in (select no ,group from B…..)
字句解释:
(select min(sage),sname from student group by sname);
1.查询数据
2.按sname分组
3.分组时可能每一组有多个值,取sage的最小值
错误一直情况
解释:select 后面的列名
1.group by的 列名
2非group by 的列名,但是需要使用聚合函数修饰.