1 / 57

AnQing Teachers College Department of Computer & Information

AnQing Teachers College Department of Computer & Information. 数据库原理与应用 Principle and Application of Database system. 安庆师范学院计算机与信息学院. 第 9 章 查询. 9.1 单表查询 9.2 连接查询 9.3 嵌套查询. 查询:从数据库中获得所需要的数据。 查询利用 SELECT 语句实现。. 语句格式 SELECT [ALL|DIsTINCT] < 目标列表达式 >[,< 目标列表达式 >] … [INTO 新表名 ]

Download Presentation

AnQing Teachers College Department of Computer & Information

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AnQing Teachers College Department of Computer & Information 数据库原理与应用 Principle and Application of Database system 安庆师范学院计算机与信息学院 Principle and Application of Database System

  2. 第9章 查询 9.1 单表查询 9.2 连接查询 9.3 嵌套查询 Principle and Application of Database System

  3. 查询:从数据库中获得所需要的数据。 • 查询利用SELECT语句实现。 Principle and Application of Database System

  4. 语句格式 • SELECT [ALL|DIsTINCT] <目标列表达式>[,<目标列表达式>] … • [INTO 新表名] • FROM <表名>[, <表名> ] … • [ WHERE <条件表达式> ] • [ GROUP BY <列名1> [,<列名2>]] • [ HAVING <条件表达式> ] • [ ORDER BY <列名3> [ AsC|DEsC ] [,<列名4>[ AsC|DEsC ] ]] Principle and Application of Database System

  5. SELECT子句:指定要显示的属性列 • INTO子句:将查询到的结果集形成一个新表 • FROM子句:指定查询对象(表) • WHERE子句:指定查询条件 • GROUP BY子句:对查询结果按指定列进行分组,该属性列值相等的元组为一个组。 • HAVING子句:筛选出只有满足指定条件的组 • ORDER BY子句:对查询结果表按指定列值的升序或降序排序 Principle and Application of Database System

  6. 主要内容 数据库的查询包括: • 单表查询 • 连接查询 • 嵌套查询 Principle and Application of Database System

  7. 示例数据库 • school数据库 student(sno,sname,ssex,sbirthday,class) teacher(tno,tname,tsex,tbirthday,prof,depart) course(cno,cname,tno) score(sno,cno,degree) Principle and Application of Database System

  8. 9.1 单表查询 查询仅涉及一个表,是一种最简单的查询操作 • 一、选择表中的若干列 • 二、选择表中的若干行 • 三、对查询结果排序 • 四、使用集函数 • 五、对查询结果分组 • 六、HAVING子句 Principle and Application of Database System

  9. 一、选择表中的若干列 1 查询指定列 2 查询全部列 3 修改查询结果中的列标题 4 替换查询结果中的数据 5 查询经过计算的值 Principle and Application of Database System

  10. 1.查询指定列 [例1] 查询全体学生的学号与姓名。 SELECT sno,sname FROM student [例2] 查询全体学生的姓名、学号、班号。 SELECT sname,sno,class FROM student Principle and Application of Database System

  11. 2.查询全部列 [例3] 查询全体学生的详细记录。 SELECT sno,sname,ssex,sage,sdept FROM student 或 SELECT * FROM student Principle and Application of Database System

  12. 3. 修改查询结果中的列标题 【例4】 查询student表中所有记录,结果中各列的标题分别指定为学号、姓名、性别、出生日期、班号。 SELECT sno AS '学号', sname AS '姓名',ssex AS'性别', sbirthday AS '出生日期',class AS '班号' FROM student '学号'=sno sno '学号' Principle and Application of Database System

  13. 4. 替换查询结果中的数据 CASE WHEN 条件1 THEN 表达式1 WHEN 条件2 THEN 表达式2 . . . ELSE 表达式n END Principle and Application of Database System

  14. 【例5】查询score表sno,sname,degree列,对degree列按以下规则进行转换;若degree为90~100,替换为“优秀”,若degree为80 ~ 89,替换为“良好”,若degree在70 ~ 79之间,替换为“中等”,若degree为60 ~ 69之间,替换为“及格”,若degree为0~ 59之间,替换为“不及格”,列标题更改为“evaluation”。 Principle and Application of Database System

  15. SELECT sno, cno, evaluation= CASE WHEN degree>=90 AND degree<=100 THEN '优秀' WHEN degree>=80 and degree<=89 THEN '良好' WHEN degree>=70 and degree<=79 THEN '中等' WHEN degree>=60 and degree<=69 THEN '及格' ELSE '不及格' END FROM score Principle and Application of Database System

  16. 5. 查询经过计算的值 SELECT子句的<目标列表达式>为表达式 • 算术表达式 • 字符串常量 • 函数 Principle and Application of Database System

  17. [例6] 查全体学生的姓名及其年龄。 SELECT sname,'age:',2000-DATEPART(yy,sbirthday) FROM student Principle and Application of Database System

  18. 二、选择表中的若干行 1 消除结果集中的重复 2 限制结果集的返回行数 3 查询满足条件的行 (1)表达式比较 (2) 指定范围 (3) 确定集合 (4) 字符匹配 (5) 空值比较 (6) 多重条件查询 Principle and Application of Database System

  19. 1. 消除结果集中的重复行 ALL与DISTINCT • [例7] 查询选修了课程的学生学号。 • (1) SELECT sno • FROM score • 或(默认 ALL) • SELECT ALL sno • FROM score Principle and Application of Database System

  20. (2) SELECT DISTINCT sno FROM score Principle and Application of Database System

  21. 2. 限制结果集的返回行数 可使用TOP选项限制查询结果集返回的行数。其基本格式为: TOP n [PERCENT] [例8] SELECT TOP 4 * FROM score SELECT TOP 40 PERCENT * FROM score Principle and Application of Database System

  22. 询 条 件 谓 词 = > < >= <= != <> !> !< 比 较 , , , , , , , , ; BETWEEN …AND… NOT BETWEEN… AND… 确定范围 , IN NOT IN 确定集合 , LIKE NOT LIKE 字符匹配 , IS NULL IS NOT NULL 空 值 , AND OR 多重条件 , 3. 查询满足条件的行 WHERE子句常用的查询条件 Principle and Application of Database System

  23. (1) 比较大小 在WHERE子句的<比较条件>中使用比较运算符 • =,>,<,>=,<=,!= 或 <>,!>,!<, • 逻辑运算符NOT + 比较运算符 [例9] 查询所有在’1975-01-01’后出生的学生学号 及其姓名。 SELECT sno,sname FROM student WHERE sbirthday>’1975-01-01’或 SELECT sno,sname FROM student WHERE NOT sbirthday <= '1975-01-01' Principle and Application of Database System

  24. (2) 确定范围 • 使用谓词 BETWEEN … AND … NOT BETWEEN … AND … [例10] 查询成绩在60~80(包括60分和80分)之间的所有记录。 SELECT * FROM score WHERE degree BETWEEN 60 AND 80 Principle and Application of Database System

  25. 例题(续) [例11] 查询成绩不在60~80之间的所有记录。 SELECT * FROM score WHERE degree NOT BETWEEN 60 AND 80 Principle and Application of Database System

  26. (3) 确定集合 IN <取值表>, NOT IN <取值表> <取值表>:用逗号分隔的一组取值 [例12]查询成绩为85、86或88的记录。 SELECT * FROM score WHERE degree IN(85,86,88) Principle and Application of Database System

  27. (3) 确定集合 [例13] 查询成绩既不是85、86,也不是88的记录。 SELECT * FROM score WHERE degree NOT IN(85,86,88) Principle and Application of Database System

  28. (4) 字符串匹配 • [NOT] LIKE ‘<匹配串>’ [ESCAPE ‘ <换码字符>’] <匹配串>:固定字符串或含通配符的字符串 当匹配串为固定字符串时: 可以用 = 运算符取代 LIKE 谓词 用 != 或 < >运算符取代 NOT LIKE 谓词 Principle and Application of Database System

  29. 例题 1) 匹配串为固定字符串 [例14] 查询学号为101的学生的详细情况。 SELECT * FROM student WHERE sno LIKE ‘101' 等价于: SELECT * FROM student WHERE sno = ‘101' Principle and Application of Database System

  30. 通配符 • % (百分号) 代表任意长度(长度可以为0)的字符串 • 例:a%b表示以a开头,以b结尾的任意长度的字符串。如acb,addgb,ab 等都满足该匹配串 • _ (下划线) 代表任意单个字符 • 例:a_b表示以a开头,以b结尾的长度为3的任意字符串。如acb,afb等都满足该匹配串 Principle and Application of Database System

  31. 例题(续) 2) 匹配串为含通配符的字符串 [例15] 查询所有姓王学生的姓名、学号和性别。 SELECT sname,sno,ssex FROM student WHERE sname LIKE '王%' Principle and Application of Database System

  32. 例题(续) [例16] 查询姓“李”且全名为二个汉字的学生的姓名。 SELECT sname FROM student WHERE sname LIKE '李_' Principle and Application of Database System

  33. 例题(续) [例17] 查询名字中第2个字为"阳"字的学生的姓名和学号。 SELECT sname,sno FROM student WHERE sname LIKE '_阳%' Principle and Application of Database System

  34. 例题(续) [例18] 查询所有不姓李的学生姓名。 SELECT sname FROM student WHERE sname NOT LIKE ‘李%' Principle and Application of Database System

  35. ESCAPE 短语: • 当用户要查询的字符串本身就含有 % 或 _ 时,要使用ESCAPE '<换码字符>' 短语对通配符进行转义。 Principle and Application of Database System

  36. 例题(续) 3) 使用换码字符将通配符转义为普通字符 [例19] 查询课程名为DB_Design课程的课程号和任课教师。 SELECT cno,tno FROM course WHERE cname LIKE 'DB\_Design' ESCAPE '\' Principle and Application of Database System

  37. 例题(续) 使用换码字符将通配符转义为普通字符(续) [例20] 查询以"DB_"开头,且倒数第3个字符为 i的课程的详细情况。 SELECT * FROM course WHERE cname LIKE 'DB\_%i_ _' ESCAPE ' \ ' Principle and Application of Database System

  38. (5) 涉及空值的查询 • 使用谓词 IS NULL 或 IS NOT NULL • “IS NULL”不能用 “= NULL”代替 [例21] 某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩。查询缺少成绩的学生的学号和相应的课程号。 SELECT sno,Cno FROM score WHERE degree IS NULL Principle and Application of Database System

  39. 例题(续) [例22] 查所有有成绩的学生学号和课程号。 SELECT sno,cno FROM score WHERE degree IS NOT NULL Principle and Application of Database System

  40. (6) 多重条件查询 用逻辑运算符AND和 OR来联结多个查询条件 • AND的优先级高于OR • 可以用括号改变优先级 Principle and Application of Database System

  41. 例题 [例23] 查询95033班且在’1975-01-01’后出生的学生姓名。 SELECT sname FROM student WHERE class= '95033' AND sbirthday>'1975-01-01' Principle and Application of Database System

  42. 改写[例10] [例10] 查询成绩在60~80(包括60分和80分)之间的所有记录。 SELECT * FROM score WHERE degree BETWEEN 60 AND 80 可改写为: SELECT * FROM score WHERE degree>=60 AND degree<=80 Principle and Application of Database System

  43. 改写[例12] [例12]查询成绩为85、86或88的记录。 SELECT * FROM score WHERE degree IN(85,86,88) 可改写为: SELECT * FROM score WHERE degree=85 OR degree=86 OR degree=88 Principle and Application of Database System

  44. 三、对查询结果排序 使用ORDER BY子句 • 可以按一个或多个属性列排序 • 升序:ASC降序:DESC缺省值为升序 当排序列含空值时 • ASC:排序列为空值的元组最先显示 • DESC:排序列为空值的元组最后显示 Principle and Application of Database System

  45. 对查询结果排序(续) [例24] 查询选修了3-105号课程的学生的学号及其成绩,查询结果按分数降序排列。 SELECT sno,degree FROM score WHERE cno= '3-105' ORDER BY degree DESC Principle and Application of Database System

  46. 对查询结果排序(续) [例25] 查询全体学生情况,查询结果按所在班的班号升序排列,同一班中的学生按出生先后顺序排列。 SELECT * FROM student ORDER BY class,sbirthday Principle and Application of Database System

  47. 四、使用集函数 5类主要集函数 • 计数 COUNT([DISTINCT|ALL] *) COUNT([DISTINCT|ALL] <列名>) • 计算总和 SUM([DISTINCT|ALL] <列名>) • 计算平均值 AVG([DISTINCT|ALL] <列名>) Principle and Application of Database System

  48. 使用集函数(续) 求最大值 MAX([DISTINCT|ALL] <列名>) 求最小值 MIN([DISTINCT|ALL] <列名>) • DISTINCT短语:在计算时要取消指定列中的重复值 • ALL短语:不取消重复值 • ALL为缺省值 Principle and Application of Database System

  49. 使用集函数 (续) [例26] 查询学生总人数。 SELECT COUNT(*) FROM student [例27] 查询选修了课程的学生人数。 SELECT COUNT(DISTINCT sno) FROM score 注:用DISTINCT以避免重复计算学生人数 Principle and Application of Database System

  50. 使用集函数 (续) [例28] 计算3-105号课程的学生平均成绩。 SELECT AVG(degree) FROM score WHERE Cno= '3-105' [例29] 查询选修3-105号课程的学生最高分数。 SELECT MAX(degree) FROM score WHER cno= '3-105' Principle and Application of Database System

More Related