5 sql sql-server join inner-join sql-server-2008
OP 对于数据库和 SQL 来说是全新的,所以这个问题可能在其他地方得到回答,但我不太有词汇来找到我要找的东西;朝着正确的方向推动会很棒。
问题是视图是空的,因为 c4 和 c5 可以为空值。
我本质上希望 c4 和 c5 上的这两个稍后的内部联接发生,如果它们之一具有值的话。
只是为了彻底:
其中每一个前面都有 UTC 和 colNum 之间的内部联接。我的意思是 UTC 和 colNum 连接总是发生。
我知道sql是一种查询语言,因此它不进行计算,但必须有一个过滤器允许将此逻辑应用于这两个表。
值得注意的是,如果 c4 存在,则 c5 为 null,如果 c5 存在,则 c4 为 null,并且如果两者都为 null,我仍然需要一行(基于前两个内部联接进行联接)。
再说一遍,我并不真正了解 SQL 相关的语言,因此我在提问之前寻找答案的努力受到了阻碍。如果类似的问题已经得到解答,请指出我。
在评论中这样做有点大,所以我将其作为答案发布。如果我对这个问题的理解是正确的,那么它会是这样的:
select *
from sizeconditionstable t1
join specalloytable t2
on (t1.c4 is not null and t2.c4 is not null and t1.c4 = t2.c4) or
(t1.c5 is not null and t2.c5 is not null and t1.c5 = t2.c5)
Run Code Online (Sandbox Code Playgroud)
编辑:
select *
from sizeconditionstable t1
join specalloytable t2
on (t1.utc = t2.utc and t1.colnum = t2.colnum) and
((t1.c4 = t2.c4) or (t1.c4 is null and t2.c4 is null)) and
((t1.c5 = t2.c5) or (t1.c5 is null and t2.c5 is null))
Run Code Online (Sandbox Code Playgroud)
这个版本将始终在utc
和colnum
c4 和 c5 上连接(如果它们都填满了两个表)。
归档时间: |
|
查看次数: |
11739 次 |
最近记录: |