数据库系统概论 An Introduction to Database System 第三章关系数据库标准语言SQL (续1) An Introduction to Database System 查询 概述 单表查询 连接查询 嵌套查询 集合查询 小结 An Introduction to Database System 连接查询 同时涉及多个表的查询称为连接查询 用来连接两个表的条件称为连接条件或连接谓词 一般格式: [<表名1>.]<列名1> <比较运算符> [<表名2>.]<列名2> 比较运算符:=、>、<、>=、<=、!= [<表名1>.]<列名1> BETWEEN [<表名2>.]<列名2> AND [<表名2>.]<列名3> An Introduction to Database System 连接查询(续) 连接字段 连接谓词中的列名称为连接字段 连接条件中的各连接字段类型必须是可比的,但不必是相同的 An Introduction to Database System 连接查询(续) SQL中连接查询的主要类型 广义笛卡尔积 等值连接(含自然连接) 非等值连接查询 自身连接查询 外连接查询 复合条件连接查询 An Introduction to Database System 一、广义笛卡尔积 不带连接谓词的连接 很少使用col S_entrance format a1 例:col sname format a10 col sdept format a10 SELECT Student.* , SC.* FROM Student, SC; An Introduction to Database System 二、等值与非等值连接查询 等值连接、自然连接、非等值连接 [例32] 查询每个学生及其选修课程的情况。 SELECT Student.*,SC.* FROM Student,SC WHERE = ; An Introduction to Database System 等值连接 连接运算符为= 的连接操作 [<表名1>.]<列名1> = [<表名2>.]<列名2> 任何子句中引用表1和表2中同名属性时,都必须加表名前缀。引用唯一属性名时可以加也可以省略表名前缀。 An Introduction to Database System 等值连接 假设Student表、SC表分别有下列数据: Student表 Sno Sname Ssex Sage Sdept 95001 李勇 男 20 CS 95002 刘晨 女 19 IS 95003 王敏 女 18 MA 95004 张立 男 19 IS An Introduction to Database System 等值连接 SC表 Sno Cno Grade 95001 1 92 95001 2 85 95001 3 88 95002 2 90 95002 3 80 An Introduction to Database System