我想在oracle中结合两个不同的表结果。 条件:
表1;
Element_ID STAMP Col1 Col2
1 22/03/2014 85 100
2 22/03/2014 95 105
表2。
Element_ID STAMP Col3 Col4
5 22/03/2014 100 110
6 22/04/2014 200 210
结果:
Element_ID STAMP Col1 Col3
1 22/03/2014 85 null
5 22/03/2014 null 100
查询:
Select Element_ID, Stamp, Col1 from Table1 where element_ID in (1, 5)
Select Element_ID, STAMP, Col3 from Table2 where Element_ID in (1,5)
如何组合上述两个查询结果,结果应符合上述格式。
答案 0 :(得分:0)
您可以尝试这样做,
Select Element_ID, Stamp, Col1, NULL Col3 from Table1 where element_ID in (1,5)
union
Select Element_ID, STAMP, NULL Col1, Col3 from Table2 where Element_ID in (1,5)
答案 1 :(得分:0)
Select T1.Element_ID,T1.Stamp, T1.Col1, T2.Col3
from Table1 T1, Table2 T2
where T1.Element_ID in (1, 5)
答案 2 :(得分:0)
这是RIGHT JOIN
在某个最小集合上的一个有趣用例 - 将结果限制为记录1和5 和以强制存在这两个记录:< / p>
SELECT "Element_ID", "STAMP", "Col1", "Col3"
FROM Table1
FULL OUTER JOIN Table2
USING("Element_ID", "STAMP")
RIGHT JOIN
(
SELECT 1 "Element_ID" FROM DUAL
UNION ALL SELECT 5 FROM DUAL
) MinimumSet
USING("Element_ID")