SQL语句使用经典例子.doc_第1页
SQL语句使用经典例子.doc_第2页
SQL语句使用经典例子.doc_第3页
SQL语句使用经典例子.doc_第4页
SQL语句使用经典例子.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

VIP免费下载

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

SQL语句使用经典例子一节、数据表的查询(select) select 字段列表 as 别名, * from 数据表名where 条件语句group by 分组字段order by 排序字段列表 descLIMIT startrow,rownumber1、Select字段列表From数据表例:、select id,gsmc,add,tel from haf(* 表示数据表中所有字段)、select 单价,数量,单价*数量 as 合计金额 from haf(As 设置字段的别名)2、Select from Where 筛选条件式筛选条件式:、字符串数据:select * from 成绩单 Where 姓名=李明、万用字符:select * from 成绩单 Where 姓名like 李%select * from 成绩单 Where 姓名like %李%select * from 成绩单 Where 姓名like %李_、特殊的条件式:= / / / / = / =AND(逻辑与)OR(逻辑或) NOT(逻辑非)Where 字段名称in(值一,值二)Where 字段名称Is Null / Where 字段名称Is Not Null3、Select from group by 字段SQL函数:SELECT sex,count(id) as women from user group by sex;函数名描述函数名描述AVG平均值Count计数MAX最大值MIN最小值Sum求和4、Select from Order by 字段列表desc(倒,如果直接写为顺序)5、Select from LIMIT .$start_rowno.,.($pagesize+1)第二节 SQL语句实例应用数据库说明:student(学生表):stdid int(11) id号son char(5) 学号sname char(20) 姓名ssex tinyint(1) 性别sage char(3) 年龄sdept char(20) 所在系course(课程表):couid int(11) id号cno char(5) 课程号cname char(20) 课程名cpno char(6) 选修课号ccredit char(50) 学分sc(学生选课表):scid int(11) id号cno char(5) 课程号grade float 成绩sno char(5) 学号单表查询:一、选择表中的若干字段:查询指定列:1、查询全体学生的学号与姓名;select son,sname from student2、查询全体学生的姓名、学号、所在系;select sname,son,sdept from student3、查询全体学生的详细记录;select * from student查询经过计算的值:4、查全体学生的姓名及其出生年份select sname,year(now()-sage as 出生年份 from student5、查询全体学生的姓名、出生年份和所有系,要求用大(小)写字母表示所有系名select sname as 姓名,出生与,year(now()-sage as 出生年份,UPPER(sdept) as 系别 from studentselect sname as 姓名,出生与,year(now()-sage as 出生年份,lower(sdept) as 系别 from student二、选择表中的若干记录:消除取值重复的行:6、查询选修了课程的学生学号select distinct sno from sc查询满足条件的记录:比较大小:7、查询计算机全体学生的名单select sname from student where sdept=cs8、查询所有年龄在20岁以下的学生姓名及其年龄select sname,sage from student where sage209、查询考试成绩小于90分的学生的学号select distinct sno from sc where grade90确定范围:10、查询年龄在18-20岁之间的学生的姓名、系别和年龄。select sname,sdept,sage from student where sage between 18 and 2011、查询年龄不在19-20岁之间的学生的姓名、系别和年龄。select sname,sdept,sage from student where sage not between 19 and 20确定集合:12、查询信息系(is)、数学系(ma)和计算机科学系(cs)学生的姓名和性别。select sname,ssex from student where sdept in(is,ma,cs)13、查询不是信息系(is)、数学系(ma)的学生的姓名、系别和年龄。select sname,ssex from student where sdept not in(is,ma)字符匹配(like %代表任意长度(长度可以为0)的字符串 ; _代表任意单个字符,汉字得用两个_):14、查询学号为95001的学生的详细情况select * from student where son like 9500115、查询所有姓名李的学生的姓名、学号和性别。select sname,son,ssex from student where sname like 李%16、查询姓名是两个字学生的姓名、学号和性别。select sname,son,ssex from student where sname like _17、查询所有不姓李的学生姓名。select sname from student where sname not like 李_涉及空值的查询:18、某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩,查询缺少成绩的学生的学号和相应的课程号。select sno,cno from sc where grade is null19、查询所有有成绩的学生学号和课程号。select sno,cno from sc where grade is not null多重条件查询(and or):20、查询计算机系年龄在20岁的学生姓名。select sname from student where sdept=cs and sage=2021、查询信息系(is)、数学系(ma)和计算机科学系(cs)学生的姓名和性别。select sname,ssex from student where sdept=is or sdept=ma or sdept=cs三、对查询结果排序:22、查询选修了3号课程的学生的学号及其成绩,查询结果按分数的降序排列。select sno,grade from sc where cno=3 order by grade desc23、查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。select * from student order by sdept,sage desc四、使用集函数:24、查询学生总人数。select count(*) as 总人数 from student25、查询选修了课程的学生人数。select count(distinct sno) as 人数 from sc26、计算1号课程的学生平均成绩select format(avg(grade),2) as 平均成绩 from sc where cno=127、查询选修1号课程的学生最高分数。select max(grade) from sc where cno=1五、对查询结果分组:28、求各个课程号及相应的选课人数。select cno as 课程号,count(sno) as 人数 from sc group by cno29、查询选修了3门以上课程的学生学号。select sno from sc group by sno having count(*)2注:where 子句与 having 短语的区别在于作用对象不同,where 子句作用于基本表或视图,从中选择满足条件的记录,having短语作用于组,从中选择满足条件的组。多表查询同时查询两个以上的表,称为连接查询。等值连接:当连接运算符为=时,为等值连接。1、查询每个学生及其选修课程的情况(等值连接)。select student.*,sc.* from student,sc where student.son=sc.sno自然连接:在等值连接中把目标列中重复的属性列去掉。2、查询每个学生及其选修课程的情况(自然连接)。select student.son,sname,ssex,sage,sdept,cno,grade from student,sc where student.son=sc.sno自身连接:连接操作不仅可以在两个表之间进行,也可以是一个表与其自己进行连接。3、查询每一门课的间接先修课。select o,b.cpno,ame from course a,course b where a.cpno=o复合条件连接:4、查询选修2号课程且成绩在90分以上的所有学生。select a.son,sname from student a,sc b where a.son=b.sno and o=2 and b.gra

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论