文档详情

db5查询表中的数据.pptx

发布:2017-05-03约2.83千字共22页下载文档
文本预览下载声明
复习;数据查询;什么是查询?;怎么查的?;SQL语法;查询记录操作;查询举例;举例1 : 查询全体学生的学号与姓名。 select sno,sname from student; 举例2 : 查询全体学生的详细记录。 select sno,sname,ssex,sage,sdept from student; 或select * from student; 举例3 : 查全体学生的姓名及其出生年份。 select sname,2008-sage from student;? 举例4 : 查询全体学生的姓名、出生年份和所有系,要求用小写字母表示所有系名。 select sname,year of birth: ,2008-sage, islower(sdept) from student; 举例5 : 查询选修了课程的学生学号(去掉重复的记录) select distinct studentid from sc;;举例6 : 查询全体学生的学号与姓名,用中文显示列名。 select sno as ‘编号’,sname as ‘姓名’ from student; 举例7 : 给表设置别名。 select s.sno,s.sname from student as s; 举例8 : 查询年龄在20以下的学生的姓名。 select sname from student where sage20;? 举例9 : 查询全体学生的姓名、年龄,要求按照年龄降序排序。 select sname,sage from student order by sage desc; 举例10 : 查询年龄最大的前3个学生的姓名和年龄,或第4、5个学生 select sname,sage from student order by sage desc limit 3;或(limit 3,2); 使用集函数;举例11 : 查询学生总数。 select count(*) from student; 举例12 : 查询选修了课程的学生人数。 select count(distinct studentid) from sc; 举例13 : 查询1号课程的学生平均成绩。 select avg(grade) from sc where courseid=1; 举例14 : 查询1号课程的学生最高分和最低分。 select max(grade) as ‘最高分’,min(grade) as ‘最低分’ from sc where courseid=1; 举例15 : 查询每个学生的平均成绩。 select studentid,avg(grade) as ‘平均成绩’ from sc group by studentid; 举例16 : 查询学生的平均成绩在70分以上的。 select studentid,avg(grade) as ‘平均成绩’ from sc group by studentid having avg(grade)70;;在WHERE子句中使用谓词 : BETWEEN AND :在两数之间 NOT BETWEEN AND :不在两数之间 IN 值表 :是否在特定的集合里(枚举) NOT IN 值表 :与上面相反 LIKE :是否匹配于一个模式 IS NULL(为空的)或 IS NOT NULL(不为空的)REGEXP : 检查一个值是否匹配一???常规表达式。;举例17 : 查询年龄在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄 。 select sname,sdept,sage from student where sage between 20 and 23; 举例18 : 查询年龄不在20~23岁之间的学生姓名、系别和年龄。 select sname,sdept,sage from student where sage not between 20 and 23; 举例19 : 查询信息系、美术系和计算机系学生的姓名和性别。 select sname,ssex from student where sdept in (‘信息系,‘美术系,‘计算机系);;举例20 : 查询学号为95001的学生的详细情况。 select * from student where sno like 95001; 等价于:select * from student where sno=95001; 举例21 : 查询所有姓刘学生的姓名、学号和性别。 select sname,sno,ssex from student
显示全部
相似文档