一.子查询:
1简单子查询语法:
select * from 表名 where 列 运算符 (子查询)
例:
select name from student where id =(select id from result where score=60)--查询分数等于60的一个学生
2.in子查询:
in子查询可以反回多个记录
语法:
select * from 表名 where 列 in (子查询)
例:
select name from student where id in(select id from result where score>60)--查询分数大于60的所有学生
3.not in子查询
not in子查询可以查询到没有达成条件的结果
语法:
select * from 表名 where 列 not in (子查询)
例:
select name from student where id not in(select id from result where score>60)--查询分数没有大于60的所有学生
4.exists子查询:
exists关键字可以检测数据是否存在.一般用于if语句的存在检测.
语法:
if exists(子查询)
例:
if exists(select * from result where subjectno=(select subjectno from subject where subjectname='Java'))
5.not exists子查询:
exists和in一样,添加not关键字可以实现取反操作,not exists表示不存在.
例:
if not exists(select * from result where subjectno=(select subjectno from subject where subjectname='Java'))