资源预览内容
第1页 / 共41页
第2页 / 共41页
第3页 / 共41页
第4页 / 共41页
第5页 / 共41页
第6页 / 共41页
第7页 / 共41页
第8页 / 共41页
第9页 / 共41页
第10页 / 共41页
亲,该文档总共41页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
多表查询 目标 通过本章学习 您将可以 使用等值和不等值连接在SELECT语句中查询多个表中的数据 使用外连接查询不满足连接条件的数据 使用自连接 从多个表中获取数据 EMPLOYEES DEPARTMENTS 笛卡尔集 笛卡尔集会在下面条件下产生 省略连接条件连接条件无效所有表中的所有行互相连接为了避免笛卡尔集 可以在WHERE加入有效的连接条件 笛卡尔集 EMPLOYEES 20行 DEPARTMENTS 8行 EquijoinNon equijoinOuterjoinSelfjoin 连接的类型 CrossjoinsNaturaljoinsUsingclauseFullortwosidedouterjoinsArbitraryjoinconditionsforouterjoins 适用于SQL 1999的连接 Oracle提供的连接 8i或更早 Oracle连接 使用连接在多个表中查询数据 在WHERE字句中写入连接条件 在表中有相同列时 在列名之前加上表名前缀 SELECTtable1 column table2 columnFROMtable1 table2WHEREtable1 column1 table2 column2 等值连接 EMPLOYEES DEPARTMENTS SELECTemployees employee id employees last name employees department id departments department id departments location idFROMemployees departmentsWHEREemployees department id departments department id 等值连接 多个连接条件与AND操作符 EMPLOYEES DEPARTMENTS 区分重复的列名 使用表名前缀在多个表中区分相同的列 使用表名可以提高效率 在不同表中具有相同列名的列可以用别名加以区分 SELECTe employee id e last name e department id d department id d location idFROMemployeese departmentsdWHEREe department id d department id 表的别名 使用别名可以简化查询 使用表名前缀可以提高执行效率 连接多个表 EMPLOYEES LOCATIONS DEPARTMENTS 连接n个表 至少需要n 1个连接条件 例如 连接三个表 至少需要两个连接条件 非等值连接 EMPLOYEES JOB GRADES 非等值连接 SELECTe last name e salary j grade levelFROMemployeese job gradesjWHEREe salaryBETWEENj lowest salANDj highest sal 外连接 EMPLOYEES DEPARTMENTS 外连接语法 使用外连接可以查询不满足连接条件的数据 外连接的符号是 SELECTtable1 column table2 columnFROMtable1 table2WHEREtable1 column table2 column SELECTtable1 column table2 columnFROMtable1 table2WHEREtable1 column table2 column SELECTe last name e department id d department nameFROMemployeese departmentsdWHEREe department id d department id 外连接 自连接 EMPLOYEES WORKER EMPLOYEES MANAGER 自连接 SELECTworker last name worksfor manager last nameFROMemployeesworker employeesmanagerWHEREworker manager id manager employee id 使用SQL 1999语法连接 使用连接从多个表中查询数据 SELECTtable1 column table2 columnFROMtable1 CROSSJOINtable2 NATURALJOINtable2 JOINtable2USING column name JOINtable2ON table1 column name table2 column name LEFT RIGHT FULLOUTERJOINtable2ON table1 column name table2 column name 叉集 使用CROSSJOIN子句使连接的表产生叉集 叉集和笛卡尔集是相同的 SELECTlast name department nameFROMemployeesCROSSJOINdepartments 自然连接 NATURALJOIN子句 会以两个表中具有相同名字的列为条件创建等值连接 在表中查询满足等值条件的数据 如果只是列名相同而数据类型不同 则会产生错误 SELECTdepartment id department name location id cityFROMdepartmentsNATURALJOINlocations 自然连接 使用USING子句创建连接 在NATURALJOIN子句创建等值连接时 可以使用USING子句指定等值连接中需要用到的列 使用USING可以在有多个列满足条件时进行选择 不要给选中的列中加上表名前缀或别名 NATURALJOIN和USING子句经常同时使用 SELECTe employee id e last name d location idFROMemployeeseJOINdepartmentsdUSING department id USING子句 使用ON子句创建连接 自然连接中是以具有相同名字的列为连接条件的 可以使用ON子句指定额外的连接条件 这个连接条件是与其它条件分开的 ON子句使语句具有更高的易读性 SELECTe employee id e last name e department id d department id d location idFROMemployeeseJOINdepartmentsdON e department id d department id ON子句 使用ON子句创建多表连接 SELECTemployee id city department nameFROMemployeeseJOINdepartmentsdONd department id e department idJOINlocationslONd location id l location id 内连接与外连接 在SQL 1999中 内连接只返回满足连接条件的数据 两个表在连接过程中除了返回满足连接条件的行以外还返回左 或右 表中不满足条件的行 这种连接称为左 或右 外联接 两个表在连接过程中除了返回满足连接条件的行以外还返回两个表中不满足条件的行 这种连接称为满外联接 SELECTe last name e department id d department nameFROMemployeeseLEFTOUTERJOINdepartmentsdON e department id d department id 左外联接 SELECTe last name e department id d department nameFROMemployeeseRIGHTOUTERJOINdepartmentsdON e department id d department id 右外联接 SELECTe last name e department id d department nameFROMemployeeseFULLOUTERJOINdepartmentsdON e department id d department id 满外联接 SELECTe employee id e last name e department id d department id d location idFROMemployeeseJOINdepartmentsdON e department id d department id ANDe manager id 149 增加连接条件
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号