2005-10-22

数据库菜鸟不可不看 简单SQL语句小结

来源: 本站收集整理 作者:佚名 评论 0 条
 

  采用别名解决问题。

  d.交叉连接

select lastname firstname from lastname CROSS JOIN firstanme

  相当于做笛卡儿积

  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))

  最外一重:从学生表中选,排除那些有课没选的。用not exist。由于讨论对象是课程,所以第二重查询从course表中找,排除那些选了课的即可。


共2页: 上一页 [1] 2 下一页
(本文仅表明作者个人观点,不代表本站及其管理员立场.) 推荐 收藏 投稿 打印 返回 关闭
上一篇:SQL语言快速入门  
下一篇:新手指南:数据库文件的后缀名
    评论加载中…
 推荐文章
     

网站首页  -  网站地图 -   站长论坛  -  网站投稿  -    -  网站管理
Copyright © 2008 芜湖站长站 All Rights Reserved 皖ICP备07500611号