| sql 语句 outer和left outer join有什么区别(转) |
使用关系代数合并数据 6z2k8i http://blog.numino.net/ 1 关系代数 THAJLK http://blog.numino.net/ 合并数据集合的理论基础是关系代数,它是由E.F.Codd于1970年提出的。 4lWU1X http://blog.numino.net/ 在关系代数的形式化语言中: q8y7R5 http://blog.numino.net/  用表、或者数据集合表示关系或者实体。 wsXI5e http://blog.numino.net/  用行表示元组。 n4TmHV http://blog.numino.net/  用列表示属性。 lwq5rc http://blog.numino.net/ 关系代数包含以下8个关系运算符 n4bEfE http://blog.numino.net/  选取――返回满足指定条件的行。 df410E http://blog.numino.net/  投影――从数据集合中返回指定的列。 W0Lbrq http://blog.numino.net/  笛卡尔积――是关系的乘法,它将分别来自两个数据集合中的行以所有可能的方式进行组合。 pc9XZM http://blog.numino.net/  并――关系的加法和减法,它可以在行的方向上合并两个表中的数据,就像把一个表垒在另一个表之上一样。 Y4LM57 http://blog.numino.net/  交――返回两个数据集合所共有的行。 SEP1xk http://blog.numino.net/  差――返回只属于一个数据集合的行。 SYQYBN http://blog.numino.net/  连接――在水平方向上合并两个表,其方法是:将两个表中在共同数据项上相互匹配的那些行合并起来。 6Rmmhe http://blog.numino.net/  除――返回两个数据集之间的精确匹配。 yTrkod http://blog.numino.net/ 此外,作为一种实现现代关系代数运算的方法,SQL还提供了: RIO9gC http://blog.numino.net/  子查询――类似于连接,但更灵活;在外部查询中,方式可以使用表达式、列表或者数据集合的地方都可以使用子查询的结果。 qSNyh9 http://blog.numino.net/ 本章将主要讲述多种类型的连接、简单的和相关的子查询、几种类型的并、关系除以及其他的内容。 fav3c8 http://blog.numino.net/ 2 使用连接 g06vpV http://blog.numino.net/ 2.1 连接类型 wsKZ61 http://blog.numino.net/ 在关系代数中,连接运算是由一个笛卡尔积运算和一个选取运算构成的。首先用笛卡尔积完成对两个数据集合的乘运算,然后对生成的结果集合进行选取运算,确保只把分别来自两个数据集合并且具有重叠部分的行合并在一起。连接的全部意义在于在水平方向上合并两个数据集合(通常是表),并产生一个新的结果集合,其方法是将一个数据源中的行于另一个数据源中和它匹配的行组合成一个新元组。 OFL168 http://blog.numino.net/ SQL提供了多种类型的连接方式,它们之间的区别在于:从相互交叠的不同数据集合中选择用于连接的行时所采用的方法不同。 AOTJbB http://blog.numino.net/ 连接类型 定义 WeqaVq http://blog.numino.net/ 内连接 只连接匹配的行 kS3S96 http://blog.numino.net/ 左外连接 包含左边表的全部行(不管右边的表中是否存在与它们匹配的行),以及右边表中全部匹配的行 zDEsmx http://blog.numino.net/ 右外连接 包含右边表的全部行(不管左边的表中是否存在与它们匹配的行),以及左边表中全部匹配的行 lGWy0p http://blog.numino.net/ 全外连接 包含左、右两个表的全部行,不管另外一边的表中是否存在与它们匹配的行。 3T8Ehg http://blog.numino.net/ (H)(theta)连接 使用等值以外的条件来匹配左、右两个表中的行 hJpF77 http://blog.numino.net/ 交叉连接 生成笛卡尔积-它不使用任何匹配或者选取条件,而是直接将一个数据源中的每个行与另一个数据源的每个行都一一匹配 LgZD7f http://blog.numino.net/ 在INFORMIX中连接表的查询 XxVr4S http://blog.numino.net/ 如果FROM子句指定了多于一个表引用,则查询会连接来自多个表的行。连接条件指定各列之间(每个表至少一列)进行连接的关系。因为正在比较连接条件中的列,所以它们必须具有一致的数据类型。 UN851I http://blog.numino.net/ SELECT语句的FROM子句可以指定以下几种类型的连接 3vRJeG http://blog.numino.net/ FROM子句关键字 相应的结果集 L7x7o2 http://blog.numino.net/ CROSS JOIN 笛卡尔乘积(所有可能的行对) 1Zcmig http://blog.numino.net/ INNER JOIN 仅对满足连接条件的CROSS中的列 N3FBxF http://blog.numino.net/ LEFT OUTER JOIN 一个表满足条件的行,和另一个表的所有行 W6zqs4 http://blog.numino.net/ RIGHT OUTER JOIN 与LEFT相同,但两个表的角色互换 78kFb3 http://blog.numino.net/ FULL OUTER JOIN LEFT OUTER 和 RIGHT OUTER中所有行的超集 aM5zNi http://blog.numino.net/ 2.2 内连接(Inner Join) gSp8OS http://blog.numino.net/ 内连接是最常见的一种连接,它页被称为普通连接,而E.FCodd最早称之为自然连接。 3HYuIc http://blog.numino.net/ 下面是ANSI SQL-92标准 3tAWXF http://blog.numino.net/ select * tlAm8G http://blog.numino.net/ from t_institution i lzvM51 http://blog.numino.net/ inner join t_teller t 57Exxm http://blog.numino.net/ on i.inst_no = t.inst_no cW6U5W http://blog.numino.net/ where i.inst_no = "5801" UVmmG6 http://blog.numino.net/ 其中inner可以省略。 qOsDV1 http://blog.numino.net/ 等价于早期的连接语法 WLJOwk http://blog.numino.net/ select * CZamI5 http://blog.numino.net/ from t_institution i, t_teller t 6Q035r http://blog.numino.net/ where i.inst_no = t.inst_no TqQ9La http://blog.numino.net/ and i.inst_no = "5801" YU6XZG http://blog.numino.net/ 2.3 外连接 P8UQPR http://blog.numino.net/ 2.3.1 左外连接(Left Outer Jion) fWy1Zd http://blog.numino.net/ select * h3MU6T http://blog.numino.net/ from t_institution i sO3ixp http://blog.numino.net/ left outer join t_teller t 6Rt59l http://blog.numino.net/ on i.inst_no = t.inst_no 6TsL0G http://blog.numino.net/ 其中outer可以省略。 B9MQu8 http://blog.numino.net/ 2.3.2 右外连接(Rigt Outer Jion) k378D2 http://blog.numino.net/ select * vX82Ma http://blog.numino.net/ from t_institution i T3eV3M http://blog.numino.net/ right outer join t_teller t xa6YJ8 http://blog.numino.net/ on i.inst_no = t.inst_no u5hdm0 http://blog.numino.net/ 2.3.3 全外连接(Full Outer) 7AvKqr http://blog.numino.net/ 全外连接返回参与连接的两个数据集合中的全部数据,无论它们是否具有与之相匹配的行。在功能上,它等价于对这两个数据集合分别进行左外连接和右外连接,然后再使用消去重复行的并操作将上述两个结果集合并为一个结果集。 wQ60T7 http://blog.numino.net/ 在现实生活中,参照完整性约束可以减少对于全外连接的使用,一般情况下左外连接就足够了。在数据库中没有利用清晰、规范的约束来防范错误数据情况下,全外连接就变得非常有用了,你可以使用它来清理数据库中的数据。 Q4IRZf http://blog.numino.net/ select * Gkb8HV http://blog.numino.net/ from t_institution i u9RJNG http://blog.numino.net/ full outer join t_teller t pIJfd3 http://blog.numino.net/ on i.inst_no = t.inst_no 47Z9NB http://blog.numino.net/ 2.3.4 外连接与条件配合使用 npjCl5 http://blog.numino.net/ 当在内连接查询中加入条件是,无论是将它加入到join子句,还是加入到where子句,其效果是完全一样的,但对于外连接情况就不同了。当把条件加入到join子句时,SQL Server、Informix会返回外连接表的全部行,然后使用指定的条件返回第二个表的行。如果将条件放到where子句中,SQL Server将会首先进行连接操作,然后使用where子句对连接后的行进行筛选。下面的两个查询展示了条件放置位子对执行结果的影响: i9377Z http://blog.numino.net/ 条件在join子句 crNfln http://blog.numino.net/ select * 0SIlL1 http://blog.numino.net/ from t_institution i c3Nes5 http://blog.numino.net/ left outer join t_teller t QD54p0 http://blog.numino.net/ on i.inst_no = t.inst_no TBUZ6V http://blog.numino.net/ and i.inst_no = “5801” C1NG8Z http://blog.numino.net/ 结果是: tX7NWt http://blog.numino.net/ inst_no inst_name inst_no teller_no teller_name 5tnnKk http://blog.numino.net/ 5801 天河区 5801 0001 tom v6E05s http://blog.numino.net/ 5801 天河区 5801 0002 david 24AoBa http://blog.numino.net/ 5802 越秀区 CmKvH2 http://blog.numino.net/ 5803 白云区 x6TQk1 http://blog.numino.net/ 条件在where子句 9ZdQzp http://blog.numino.net/ select * Q0AIL2 http://blog.numino.net/ from t_institution i 55va8T http://blog.numino.net/ left outer join t_teller t 3LoR0r http://blog.numino.net/ on i.inst_no = t.inst_no DWqYqz http://blog.numino.net/ where i.inst_no = “5801” Dwy48F http://blog.numino.net/ 结果是: O1SR3D http://blog.numino.net/ inst_no inst_name inst_no teller_no teller_name 3Y9bUM http://blog.numino.net/ 5801 天河区 5801 0001 tom uL9NGV http://blog.numino.net/ 5801 天河区 5801 0002 david 1s3J1g http://blog.numino.net/ 2.4 自身连接 Oo3lTq http://blog.numino.net/ 自身连接是指同一个表自己与自己进行连接。这种一元连接通常用于从自反关系(也称作递归关系)中抽取数据。例如人力资源数据库中雇员与老板的关系。 u8ip3h http://blog.numino.net/ 下面例子是在机构表中查找本机构和上级机构的信息。 2qroog http://blog.numino.net/ select s.inst_no superior_inst, s.inst_name sup_inst_name, i.inst_no, i.inst_name 81RxWM http://blog.numino.net/ from t_institution i DUP1Gf http://blog.numino.net/ join t_institution s SJla82 http://blog.numino.net/ on i.superior_inst = s.inst_no 5NGY8r http://blog.numino.net/ 结果是: q4Resl http://blog.numino.net/ superior_inst sup_inst_name inst_no inst_name Ai4x3H http://blog.numino.net/ 800 广州市 5801 天河区 5KQW6D http://blog.numino.net/ 800 广州市 5802 越秀区 1LiTE7 http://blog.numino.net/ 800 广州市 5803 白云区 cA9u7X http://blog.numino.net/ 2.5 交叉(无限制) 连接 e4xv0M http://blog.numino.net/ 交叉连接用于对两个源表进行纯关系代数的乘运算。它不使用连接条件来限制结果集合,而是将分别来自两个数据源中的行以所有可能的方式进行组合。数据集合中一的每个行都要与数据集合二中的每一个行分别组成一个新的行。例如,如果第一个数据源中有5个行,而第二个数据源中有4个行,那么在它们之间进行交叉连接就会产生20个行。人们将这种类型的结果集称为笛卡尔乘积。 hjJ1Gg http://blog.numino.net/ 大多数交叉连接都是由于错误操作而造成的;但是它们却非常适合向数据库中填充例子数据,或者预先创建一些空行以便为程序执行期间所要填充的数据保留空间。 94dw88 http://blog.numino.net/ select * tZtwHr http://blog.numino.net/ from t_institution i qa82xs http://blog.numino.net/ cross join t_teller t 7V02Qu http://blog.numino.net/ 在交叉连接中没有on条件子句 uvwnI7 http://blog.numino.net/ mMFUhl http://blog.numino.net/ W7F3b1 http://blog.numino.net/
|
|