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

来源:互联网 发布:ib盈透 知乎 编辑:程序博客网 时间:2024/05/04 22:43
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

  为了大家更容易理解我举出的SQL语句,本文假定已经建立了一个学生成绩管理数据库,全文均以学生成绩的管理为例来描述。

  1.在查询结果中显示列名:

  a.用as关键字:selectnameas'姓名'fromstudentsorderbyage

  b.直接表示:selectname'姓名'fromstudentsorderbyage

  2.精确查找:

  a.用in限定范围:select*fromstudentswherenativein('湖南','四川')

  b.between...and:select*fromstudentswhereagebetween20and30

  c.“=”:select*fromstudentswherename='李山'

  d.like:select*fromstudentswherenamelike'李%'(注意查询条件中有“%”,则说明是部分匹配,而且还有先后信息在里面,即查找以“李”开头的匹配项。所以若查询有“李”的所有对象,应该命令:'%李%';若是第二个字为李,则应为'_李%'或'_李'或'_李_'。)

  e.[]匹配检查符:select*fromcourseswherecnolike'[AC]%'(表示或的关系,与"in(...)"类似,而且"[]"可以表示范围,如:select*fromcourseswherecnolike'[A-C]%')

  3.对于时间类型变量的处理

  a.smalldatetime:直接按照字符串处理的方式进行处理,例如:
select*fromstudentswherebirth>='1980-1-1'andbirth<='1980-12-31'

  4.集函数

  a.count()求和,如:selectcount(*)fromstudents(求学生总人数)

  b.avg(列)求平均,如:selectavg(mark)fromgradeswherecno=’B2’

  c.max(列)和min(列),求最大与最小

  5.分组group

  常用于统计时,如分组查总数:
selectgender,count(sno)
fromstudents
groupbygender
(查看男女学生各有多少)

  注意:从哪种角度分组就从哪列"groupby"

  对于多重分组,只需将分组规则罗列。比如查询各届各专业的男女同学人数,那么分组规则有:届别(grade)、专业(mno)和性别(gender),所以有"groupbygrade,mno,gender"

selectgrade,mno,gender,count(*)
fromstudents
groupbygrade,mno,gender

  通常group还和having联用,比如查询1门课以上不及格的学生,则按学号(sno)分类有:

selectsno,count(*)fromgrades
wheremark<60
groupbysno
havingcount(*)>1

  6.UNION联合

  合并查询结果,如:

SELECT*FROMstudents
WHEREnamelike‘张%’
UNION[ALL]
SELECT*FROMstudents
WHEREnamelike‘李%’

  7.多表查询

  a.内连接

selectg.sno,s.name,c.coursename
fromgradesgJOINstudentssONg.sno=s.sno
JOINcoursescONg.cno=c.cno
(注意可以引用别名)
b.外连接
b1.左连接
selectcourses.cno,max(coursename),count(sno)
fromcoursesLEFTJOINgradesONcourses.cno=grades.cno
groupbycourses.cno

  左连接特点:显示全部左边表中的所有项目,即使其中有些项中的数据未填写完全。

  左外连接返回那些存在于左表而右表中却没有的行,再加上内连接的行。

  b2.右连接

  与左连接类似

  b3.全连接

selectsno,name,major
fromstudentsFULLJOINmajorsONstudents.mno=majors.mno

  两边表中的内容全部显示

  c.自身连接

selectc1.cno,c1.coursename,c1.pno,c2.coursename
fromcoursesc1,coursesc2wherec1.pno=c2.cno

  采用别名解决问题。

  d.交叉连接

selectlastname+firstnamefromlastnameCROSSJOINfirstanme

  相当于做笛卡儿积

  8.嵌套查询

  a.用关键字IN,如查询李山的同乡:

select*fromstudents
wherenativein(selectnativefromstudentswherename=’李山’)

  b.使用关键字EXIST,比如,下面两句是等价的:1

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>