我有两个表,其中有些列相似,有些列不同。我需要返回一个将不同列合并到一个结果集中的结果,但是,我需要一个可能导致两个表中的任何一个都不匹配的条件,我尝试了一个联合,但它返回了具有空值的两行,并且我只想要一个。下面是两个示例表:
TableA
----------------------------------------------------
| ID | ColumnA | ColumnB | ForeignKeyA | TimeStamp |
----------------------------------------------------
| 1 | Val1 | Val2 | KeyA | 2013-01-01|
----------------------------------------------------
| 2 | Val3 | Val4 | KeyB | 2013-01-02|
----------------------------------------------------
TableB
------------------------------------------
| ID | ColumnC | ForeignKeyA | TimeStamp |
------------------------------------------
| 1 | Val5 | KeyA | 2013-01-01|
------------------------------------------
| 2 | Val6 | KeyC | 2013-01-02|
------------------------------------------
这里有一些伪查询和我想要的返回值:
1)
SELECT TableA.ColumnA AS ColumnA,
TableA.ColumnB AS Column B,
TableB.ColumnC AS ColumnC,
TableA.id AS TableA_ID,
TableB.id AS TableB_ID
(WHERE ForeignKeyA in either table = KeyA and TimeStamp in either table = 2013-01-01)
>>
-------------------------------------------------------
| ColumnA | ColumnB | ColumnC | TableA_ID | TableB_ID |
-------------------------------------------------------
| Val1 | Val2 | Val5 | 1 | 1 |
-------------------------------------------------------
2)
SELECT TableA.ColumnA AS ColumnA,
TableA.ColumnB AS Column B,
TableB.ColumnC AS ColumnC,
TableA.id AS TableA_ID,
TableB.id AS TableB_ID
(WHERE ForeignKeyA in either table = KeyB and TimeStamp in either table = 2013-01-02)
>>
-------------------------------------------------------
| ColumnA | ColumnB | ColumnC | TableA_ID | TableB_ID |
-------------------------------------------------------
| Val3 | Val4 | Null | 2 | Null |
-------------------------------------------------------
3)
SELECT TableA.ColumnA AS ColumnA,
TableA.ColumnB AS Column B,
TableB.ColumnC AS ColumnC,
TableA.id AS TableA_ID,
TableB.id AS TableB_ID
(WHERE ForeignKeyA in either table = KeyC and TimeStamp in either table = 2013-01-02)
>>
-------------------------------------------------------
| ColumnA | ColumnB | ColumnC | TableA_ID | TableB_ID |
-------------------------------------------------------
| Null | Null | Val6 | Null | 2 |
-------------------------------------------------------
最佳答案
我认为在您的查询中您可以像这样使用完全外部联接
(您可以将列添加到on
子句中,但它不会改变任何内容):
with
cteA as (select * from TableA where TimeStamp = _ts and ForeignKeyA = _fk),
cteB as (select * from TableB where TimeStamp = _ts and ForeignKeyA = _fk)
select
A.ColumnA, A.ColumnB, B.ColumnC,
A.ID as TableA_ID, B.ID as TableB_ID
from cteA as A
full outer join cteB as B on 1 = 1
但是您可以在不进行预过滤的情况下使用完全外部联接
(如果您在ForeignKeyA
和TimeStamp
列上有索引,这会降低效率):
select
A.ColumnA, A.ColumnB, B.ColumnC,
A.ID as TableA_ID, B.ID as TableB_ID
from TableA as A
full outer join TableB as B
on B.ForeignKeyA = A.ForeignKeyA and B.TimeStamp = A.TimeStamp
where
coalesce(A.ForeignKeyA, B.ForeignKeyA) = _fk and
coalesce(A.TimeStamp, B.TimeStamp) = _ts
关于当任一表可能返回零结果时,SQL 从多个表返回一个结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19099551/