OP是一个完全陌生的数据库和SQL,所以这个问题可能在其他地方得到了回答,但我没有足够的词汇量来找到我想要的东西;如果能在正确的方向上推一把,那就太好了。
问题是视图是空的,因为c4和c5可以是空值。
我本质上是想让这两个表在c4和c5上的内联接发生在其中一个有值的情况下。
只是为了彻底。
每一条前面都有UTC和colNum的内部连接,我的意思是UTC和colNum的连接总是发生。
我知道sql是一种查询语言,所以它不进行计算,但必须有一个过滤器,允许将这种逻辑应用于这两个表。
有用的是,如果c4存在,c5是空的,如果c5存在,c4是空的,如果这两个表都是空的,我还是要一行(根据前面两个内联接连接)。
同样,我并不真正了解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)
编辑:
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))
这是一个会一直加入的版本 utc
和 colnum
以及c4和c5,如果它们在两个表中都被填满的话。
coalesce(column1, column2, '')可以工作,但是,它并不便宜,所以如果你要连接两个大表,你最好采用不同的方法。
我认为UNION ALL查询是你最好的选择。
SELECT field1, field2, field3
FROM table1 t1
JOIN table2 t2 ON t2.field1 = t1.field1 AND t2.field2 = t2.field2
UNION ALL
SELECT field1, field2, field3
FROM table1 t1
JOIN table2 t2 ON t2.field1 = t1.field1 AND t2.field3 = t2.field3
在这种情况下,如果字段2是空的,而字段3不是,那么第一个查询就没有结果,但是第二个查询会带来结果。
我重新阅读了你的请求。 联合查询是合适的解决方案。 沿着这条线的东西应该很好用。
select *
from
(
select
t1.primaryKey,
t2.foreignKey
from
table1 t1 inner join table2 t2 on t1.c1 = t2.c1
union
select
t1.primaryKey,
t2.foreignKey
from
table1 t1 inner join table2 t2 on t1.c2 = t2.c2
) x inner join table1 t1 on x.primaryKey = t1.primaryKey
inner join table2 t2 on x.foreignKey