查看: 2164|回复: 3

1、如何理解这样的“连接”2、标准连接就不如“(+)”吗?

[复制链接]
论坛徽章:
2
授权会员
日期:2005-10-30 17:05:33ITPUB新首页上线纪念徽章
日期:2007-10-20 08:38:44
跳转到指定楼层
1#
发表于 2005-4-6 18:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
一、问题起源。
疑惑起源于看到一个SQL里有这样的一段表达式
AND A.CARD_ID = B.CARD_ID(+)
AND B.DEPT_ID(+)[/COLOR]  IS NULL
AND B.ORGAN_ID(+)[/COLOR] = '1'

“B.DEPT_ID(+) IS NULL”和“B.ORGAN_ID(+) = '1'”,怎么这里面也会有“(+)”加号呢?

二、实验。
我做了实验。假设A、B两个表。
A.TITLE   B.TITLE
-------   -------
    1           1
    2           2
    3           4

1、常见的外连接(左连接)
SQL> select * from A,B
  2  where A.title = B.title(+);
TITLE TITLE
----- -----
    1     1
    2     2
    3

2、我看不懂的写法。B.title(+) = 1
SQL> select * from A,B
  2  where A.title = B.title(+);
  3  and B.title(+) = 1;

TITLE TITLE
----- -----
    1     1[/COLOR]
    2
    3

3、再来一种写法,B.title = 1,就开始搞脑子了。
SQL> select * from A,B
  2  where A.title = B.title(+);
  3  and B.title = 1;

TITLE TITLE
----- -----
    1     1
大家能谈谈 2和3的执行顺序吗?从逻辑上我们怎么去一步一步的推断执行的结果呢?

三、又想到了标准SQL。
接着问题又来了。
现在都推荐写标准SQL语句了。9i也支持SQL99标准的连接写法。
看,干净利落的左连接。
SQL> select t1.title,t2.title
  2  from test_join_1 t1 LEFT OUTER JOIN test_join_2 t2
  3       ON (t1.title=t2.title);

TITLE TITLE
----- -----
    1     1
    2     2
    3
问题是,怎么使用标准SQL来达到前面实验中第2点所表现的结果呢?
当然,可以通过decode函数。
SQL> select t1.title,decode(t2.title,1,t2.title,NULL)
  2  from test_join_1 t1 LEFT OUTER JOIN test_join_2 t2
  3  ON (t1.title=t2.title);

TITLE DECODE(T2.TITLE,1,T2.TITLE,NUL
----- ------------------------------
    1                              1
    2
    3
不过decode函数好像也不是标准函数。
所以再请大家谈谈有何见解?是不是用标准的SQL就是不能了?

谢谢。
论坛徽章:
2
授权会员
日期:2005-10-30 17:05:33ITPUB新首页上线纪念徽章
日期:2007-10-20 08:38:44
2#
 楼主| 发表于 2005-4-12 22:45 | 只看该作者

自己顶一下

使用道具 举报

回复
论坛徽章:
131
2006年度最佳技术回答
日期:2007-01-24 12:58:48福特
日期:2013-10-24 13:57:422014年新春福章
日期:2014-02-18 16:41:11马上有车
日期:2014-02-18 16:41:11马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上加薪
日期:2014-02-19 11:55:142013年新春福章
日期:2013-02-25 14:51:24
3#
发表于 2005-4-13 08:57 | 只看该作者
B.title(+) = 1
==>
b.title = 1 or b.title is null

A.title = B.title(+)
and B.title = 1
==>
a.title = b.title or exist a.title but  b.title does not exist
and  B.title = 1
结合起来
等于a.title = b.title

A.title = B.title(+)
and B.title(+) = 1
a.title = b.title or exist a.title but  b.title does not exist
and  B.title = 1 or   b.title is null
相当于
(a.title = b.title and b.title = 1) or ( not exist b.title )

使用道具 举报

回复
论坛徽章:
10
授权会员
日期:2005-10-30 17:05:332010年世界杯参赛球队:科特迪瓦
日期:2010-04-15 12:20:472010年世界杯参赛球队:智利
日期:2010-04-13 17:15:21生肖徽章2007版:蛇
日期:2009-09-24 13:54:11生肖徽章2007版:龙
日期:2009-09-22 13:56:012009日食纪念
日期:2009-07-22 09:30:00生肖徽章2007版:龙
日期:2009-02-10 13:45:15生肖徽章2007版:狗
日期:2009-02-03 13:53:34会员2006贡献徽章
日期:2006-04-17 13:46:34ITPUB十周年纪念徽章
日期:2011-11-01 16:20:28
4#
发表于 2005-4-13 09:42 | 只看该作者
Outer Joins
An outer join extends the result of a simple join. An outer join returns all rows that satisfy the join condition and those rows from one table for which no rows from the other satisfy the join condition. Such rows are not returned by a simple join. To write a query that performs an outer join of tables A and B and returns all rows from A, apply the outer join operator (+) to all columns of B in the join condition. For all rows in A that have no matching rows in B, Oracle returns null for any select list expressions containing columns of B.

Outer join queries are subject to the following rules and restrictions:

n The (+) operator can appear only in the WHERE clause or, in the context of leftcorrelation (that is, when specifying the TABLE clause) in the FROM clause, and can be applied only to a column of a table or view.

n If A and B are joined by multiple join conditions, you must use the (+) operator in all of these conditions. If you do not, Oracle will return only the rows resulting from a simple join, but without a warning or error to advise you that you do not have the results of an outer join.

n The (+) operator can be applied only to a column, not to an arbitrary expression. However, an arbitrary expression can contain a column marked with the (+) operator.

n A condition containing the (+) operator cannot be combined with another
condition using the OR logical operator.

n A condition cannot use the IN comparison operator to compare a column marked with the (+) operator with an expression.

n A condition cannot compare any column marked with the (+) operator with a subquery.

If the WHERE clause contains a condition that compares a column from table B with a constant, the (+) operator must be applied to the column so that Oracle returns the rows from table A for which it has generated NULLs for this column. Otherwise Oracle will return only the results of a simple join.

In a query that performs outer joins of more than two pairs of tables, a single table can be the null-generated table for only one other table. For this reason, you cannot apply the (+) operator to columns of B in the join condition for A and B and the join condition for B and C.

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表