本文共 2072 字,大约阅读时间需要 6 分钟。
1.什么是单个数据点?
数据点就是数据表中的每一个空格内的数据,都可以简称为数据点。 例如: select Name from TA where Id=1 2.什么是一列数据? 一列数据就是一个列名下的一列数据; 例如:select Id from TA where Type=‘酒类’//type在数据库里可以理解为列名 3.where子查询 #例题:查询出学生”张耀仁”的同班同学 通过子查询,子查询查询到张耀仁的班级编号,在通过这个班级编号来从父查询进行查询 select studentname from studentinfo where classid=(select classid from studentinfo where studentname=‘张耀仁’); 4.inner join on 和子查询的区别 例题:考试刚好等于55分的学生名单学习课程是javaselect * from studentinfo s inner join exam e on s.studentid=e.studentid inner join subjectinfo sub on e.subjectid=sub.subjectid where e.exam=55 and sub.subjectname=‘java’;
#切记 子查询跟比较运算符使用的时候 最好要确保数据只有一条,如果大于一条了 会提示子查询返回多行
select studentname from studentinfo where studentid=(select studentid from exam where exam=55 and subjectid=(select subjectid from subjectinfo where subjectname=‘java’)); 特别注意:子查询内如果有运算比较符的话 那么要确保他要查询的内容只是一条如果子查询返回多条数据的话 就会报错。和 not in;
例题:考试刚好等于55分的学生名单学习课程是java 通过in来演示select studentname from studentinfo where studentid in(select studentid from exam where exam=55 and subjectid in(select subjectid from subjectinfo where subjectname=‘java’));
not in 和in想要的效果相反. 6.EXISTS 例题:查询成绩表中科目编号为2的考试成绩中是否存在不及格的学生,如果存在不及格的学生就将参加科目编号2考试的学生编号和成绩全部查询显示出来。select * from exam where subjectid=2 and exists(select studentid from exam where exam=<60)
7.ALL、ANY/SOME的子查询
ALL用在子查询前,通过比较运算符将一个表达式的值或列值与子查询返回的一列值中的每一行进行比较,只要有一次比较的结果为 FALSE,则 ALL测试返回FALSE。ALL要求子查询必须返回而且只能返回一个字段。 语法:表达式或字段 比较运算符 ALL(子查询);ANY与子查询在一起使用时,按照比较运算符、表达式或字段对子查询的结果的每一行进行一次计算和比较。只要有一次满足条件,那么ANY的结果就是真。当子查询每行的结果与ANY前面的表达式或字段比较结果全为假时,则结果为假。
语法:表达式或字段 比较运算符 ANY/SOME(子查询)转载地址:http://yjnlf.baihongyu.com/