我有两个表,其中一些列相似,一些列不同。我需要返回一个将不同列合并到一个结果集中的结果,但是,我需要一个可能导致任何一个表都没有匹配项的条件,我尝试了一个联合,但它返回了两行具有空值的行,并且我想要一个。以下是两个示例表:
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 |
-------------------------------------------------------