SQL OUTER JOIN:需要“部分 NULL”行
我正在寻找一种方法来归档以下内容:
想象表 A、B:
A:
aID, aID2, avalue
=================
1 , 10 , 'abc'
2 , 20 , 'def'
3 , 30 , 'ghi'
4 , 40 , 'jkl'
B:
bID, bID2, bvalue
=================
1 , 10 , 'mno'
20 , 20 , 'pqr'
3 , 1 , 'stu'
现在查看以下 SQL 语句和结果(我使用的是 Oracle 11,但对于 MSSQL 应该是相同的):
SELECT A .*, B.* 从左外连接 B ON (A.aID = B.bID)
aID, aID2, avalue, bID , bID2, bvalue
=====================================
1 , 10 , 'abc' , 1 , 10 , 'mno'
2 , 20 , 'def' , NULL, NULL, NULL
3 , 30 , 'ghi' , 3 , 1 , 'stu'
4 , 40 , 'jkl' , NULL, NULL, NULL
从左外连接 B ON (A.aID = B.bID) 选择 A.*, B.* B.bID AND A.aID2 = B.bID2)
aID, aID2, avalue, bID , bID2, bvalue
=====================================
1 , 10 , 'abc' , 1 , 10 , 'mno'
2 , 20 , 'def' , NULL, NULL, NULL
3 , 30 , 'ghi' , NULL, NULL, NULL
4 , 40 , 'jkl' , NULL, NULL, NULL
到目前为止还好。
我正在寻找一个语句(尽可能简单),它让我得到以下内容:
MADE-UP-CODE: SELECT A.*, B.* FROM A LEFT OUTER JOIN B ON (A.aID = B.bID AND A.aID2 = B.bID2 KEEP MATCHING COLS)
aID, aID2, avalue, bID , bID2, bvalue
=====================================
1 , 10 , 'abc' , 1 , 10 , 'mno'
2 , 20 , 'def' , NULL, 20 , NULL (note 20)
3 , 30 , 'ghi' , 3 , NULL, NULL (note 3)
4 , 40 , 'jkl' , NULL, NULL, NULL
有没有办法在不使用连接的情况下仅使用连接来获得此行为(保持匹配部分,NULL 不匹配“ON”子句和所有值列的部分)一遍又一遍地自加入?
如果没有像“KEEP MATCHING COLS”这样的关键世界,你会建议什么方法? 子选择?自加入?
谢谢, 布拉马
I'm looking for a way to archive the following:
Imagine Tables A, B:
A:
aID, aID2, avalue
=================
1 , 10 , 'abc'
2 , 20 , 'def'
3 , 30 , 'ghi'
4 , 40 , 'jkl'
B:
bID, bID2, bvalue
=================
1 , 10 , 'mno'
20 , 20 , 'pqr'
3 , 1 , 'stu'
Now look at the following SQL statement and results (I'm on Oracle 11, but should be the same for MSSQL):
SELECT A.*, B.* FROM A LEFT OUTER JOIN B ON (A.aID = B.bID)
aID, aID2, avalue, bID , bID2, bvalue
=====================================
1 , 10 , 'abc' , 1 , 10 , 'mno'
2 , 20 , 'def' , NULL, NULL, NULL
3 , 30 , 'ghi' , 3 , 1 , 'stu'
4 , 40 , 'jkl' , NULL, NULL, NULL
SELECT A.*, B.* FROM A LEFT OUTER JOIN B ON (A.aID = B.bID AND A.aID2 = B.bID2)
aID, aID2, avalue, bID , bID2, bvalue
=====================================
1 , 10 , 'abc' , 1 , 10 , 'mno'
2 , 20 , 'def' , NULL, NULL, NULL
3 , 30 , 'ghi' , NULL, NULL, NULL
4 , 40 , 'jkl' , NULL, NULL, NULL
Fine so far.
I'm looking for a statement (as easy as possible), that gets me the following:
MADE-UP-CODE: SELECT A.*, B.* FROM A LEFT OUTER JOIN B ON (A.aID = B.bID AND A.aID2 = B.bID2 KEEP MATCHING COLS)
aID, aID2, avalue, bID , bID2, bvalue
=====================================
1 , 10 , 'abc' , 1 , 10 , 'mno'
2 , 20 , 'def' , NULL, 20 , NULL (note 20)
3 , 30 , 'ghi' , 3 , NULL, NULL (note 3)
4 , 40 , 'jkl' , NULL, NULL, NULL
Is there a way to get this behavior (keep matching parts, NULL not matching parts of "ON" clause and all value columns) using only joins while not using self-joins over and over?
What way would you suggest if there is no keyworld like "KEEP MATCHING COLS"?
Subselect? Selfjoins?
Thanks,
Blama
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
连接 Id 或 Id2,然后有选择地清空 select 子句中的结果。
设置测试表和数据:
查询:
结果:
Join on Id or Id2 and then selectively null out the results in the select clause.
Set up test tables and data:
Query:
Results:
我认为您不会找到一个简单的解决方案,这里有一些适用于您的数据集的东西,但不漂亮或高效!
不完全是自加入,但也没有更好,我有兴趣看到更好的解决方案并了解要求。
I don't think you are going to find an easy solution to this, here is something that works on your data set, but isn't pretty or efficient!
Not quite self joins, but no better, i'd be interested in seeing a better solution and also understanding the requirement.
不知道为什么你不能使用/不想要自连接,但这是一个版本:
结果:
Not sure why you can't use/don't want self joins, but here's a version:
Results:
为了使这个更容易编写(并因此维护),我建议您避免外部联接,而是联合您需要的四个子集,例如
这种方法的优点是使用关系运算符联接、半差和联合,允许那些非关系运算符
NULL
值(外连接专门用于生成)可以轻松替换为实际的默认值。To make this easier to write (and therefore maintain), I suggest you avoid outer join and instead union the four subsets you require e.g.
The advantage to this approach is that is uses relational operators join, semi difference and union, allowing those non-relational
NULL
values (which outer join is expressly designed to generate) to be easily replaced with actual default values.