查询:
1、相关子查询:
涵义:子查询的返回结果,与外部查询的每一行都是相关的。
执行步骤:先执行外部查询 ---> 对外部查询的每一行,子查询分别执行一次。
效率:相关子查询效率比较低,索所以尽量用表连接来替代。
2、非相关子查询:
涵义:子查询的值与外部查询的值是不相关的。
3、嵌套查询:
语法:select * from (查询语句) 表别名。
用于:分页
4、条件子查询(运算符):
select * from 成绩表 where 分数=89
--->返回的是分数为89的列值
select * from 成绩表 where 分数 in (98,89)
--->in 相当为一个范围
select * from 成绩表 where 分数=(select max (分数) from 成绩表)
---> 返回的是一个最大值
select * from 班级表 where 班级编号=(select distinct 班级编号 from 学生表)
---> 子查询返回的值多于一个。当子查询跟随在 =、!=、、>= 之后,或子查询用作表达式时,这种情况是不允许的。
select * from 班级表 where 班级编号 in (select distinct 班级编号 from 学生表)
select * from 班级表 where 班级编号=(1,2)
---> 这样运行也不行的 错误同上
select * from 班级表 where 班级编号 in (1,2)
等价于
select * from 班级编号 where 班级编号=1 or 班级编号=2s
select * from 成绩表 where 科目='语文' and 分数 增加一个最高人数列
等价于
select 班级编号,班级名称,30 as 最高人数 from 班级表
union all 将所有的数据串起来(数据类型必须相同)
union 显示不重复数据
以下两种只被oracle支持
intersect 显示交集
minus 显示两张表中另一张表没有的数据
5、连接
表连接:
select * from 表1,表2;
等价于(数据冗余)
select * from 表1 cross join 表2
相等连接 :
select * from 表1,表2 where 表1.id = 表2.id
等价于
select * from 表1 join 表2 on 表1.id =表2.id
外连接:(left、right、full)
select * from 表1 left /right/full join 表2 on 表1.id = 表2.id