简单的SQL语句学习资料 经典SQL语句(下)
2007-12-19 15:14
435 查看
相当于做笛卡儿积
8.嵌套查询
a.用关键字IN,如查询李山的同乡:
select * from students
where native in (select native from students where name=’ 李山’)
b.使用关键字EXIST,比如,下面两句是等价的:
select * from students
where sno in (select sno from grades where cno=’B2’)
select * from students where exists
(select * from grades where
grades.sno=students.sno AND cno=’B2’)
9.关于排序order
a.对于排序order,有两种方法:asc升序和desc降序
b.对于排序order,可以按照查询条件中的某项排列,而且这项可用数字表示,如:
select sno,count(*) ,avg(mark) from grades
group by sno
having avg(mark)>85
order by 3
10.其他
a.对于有空格的识别名称,应该用"[]"括住。
b.对于某列中没有数据的特定查询可以用null判断,如select sno,courseno from grades where mark IS NULL
c.注意区分在嵌套查询中使用的any与all的区别,any相当于逻辑运算“||”而all则相当于逻辑运算“&&”
d.注意在做否定意义的查询是小心进入陷阱:
如,没有选修‘B2’课程的学生 :
select students.*
from students, grades
where students.sno=grades.sno
AND grades.cno <> ’B2’
上面的查询方式是错误的,正确方式见下方:
select * from students
where not exists (select * from grades
where grades.sno=students.sno AND cno='B2')
11.关于有难度多重嵌套查询的解决思想:
如,选修了全部课程的学生:
select *
from students
where not exists ( select *
from courses
where NOT EXISTS
(select *
from grades
where sno=students.sno
AND cno=courses.cno))
http://news.newhua.com/html/Deve_data/2007-3/9/073910391283149_62_2.shtml
8.嵌套查询
a.用关键字IN,如查询李山的同乡:
select * from students
where native in (select native from students where name=’ 李山’)
b.使用关键字EXIST,比如,下面两句是等价的:
select * from students
where sno in (select sno from grades where cno=’B2’)
select * from students where exists
(select * from grades where
grades.sno=students.sno AND cno=’B2’)
9.关于排序order
a.对于排序order,有两种方法:asc升序和desc降序
b.对于排序order,可以按照查询条件中的某项排列,而且这项可用数字表示,如:
select sno,count(*) ,avg(mark) from grades
group by sno
having avg(mark)>85
order by 3
10.其他
a.对于有空格的识别名称,应该用"[]"括住。
b.对于某列中没有数据的特定查询可以用null判断,如select sno,courseno from grades where mark IS NULL
c.注意区分在嵌套查询中使用的any与all的区别,any相当于逻辑运算“||”而all则相当于逻辑运算“&&”
d.注意在做否定意义的查询是小心进入陷阱:
如,没有选修‘B2’课程的学生 :
select students.*
from students, grades
where students.sno=grades.sno
AND grades.cno <> ’B2’
上面的查询方式是错误的,正确方式见下方:
select * from students
where not exists (select * from grades
where grades.sno=students.sno AND cno='B2')
11.关于有难度多重嵌套查询的解决思想:
如,选修了全部课程的学生:
select *
from students
where not exists ( select *
from courses
where NOT EXISTS
(select *
from grades
where sno=students.sno
AND cno=courses.cno))
http://news.newhua.com/html/Deve_data/2007-3/9/073910391283149_62_2.shtml
相关文章推荐
- 简单的SQL语句学习资料 经典SQL语句
- 简单的SQL语句学习资料 经典SQL语句
- 简单的SQL语句学习资料 经典SQL语句(上)
- 学习资料 经典SQL语句大全
- 一条语句简单解决“每个Y的最新X”的经典sql语句
- 经典SQL语句资料大全之高手之路
- 经典SQL语句大全--------------学习笔记
- oracle学习---简单的sql语句查询
- 一条语句简单解决“每个Y的最新X”的经典sql语句
- 经典SQL语句资料大全高手必备
- 学习资料 50个常用的sql语句
- 【oracle资料】oracle sql语句 基础 学习
- 经典SQL语句大全 收集别人的主要是为了学习的时候查找方便
- 经典SQL学习笔记 (一)-简单、限定、模糊、排序
- 黑马程序员_学习日记48_616数据库开发及ADO.Net(带参数的Sql语句、数据库与文本文件导入导出、省市联动、资料管理器、DataSet (ado.net断开式数据访问)、SQLHelper)
- Oracle学习总结(5)—— SQL语句经典案例
- 经典SQL语句资料大全之开发案例
- Oracle学习总结(5)—— SQL语句经典案例
- SQL -- 简单语句学习总结7条
- 我的学习笔记(SQL简单的注入)(1) 手工注入以及注入一些常用语句