Oracle连接查询
我有两张桌子Oracle连接查询,oracle,join,inner-join,Oracle,Join,Inner Join,我有两张桌子 表A有列id | name | age 表B有列id | name | age 表A中的样本记录 1|xavi |23 2|christine|24 3|faisal |25 5|jude |27 表B中的样本记录 1|xavi |23 2|christine|22 3|faisal |23 4|ram |25 如果表A中的id值与表B中的匹配,则仅从表A中获取记录。 同时,只记录表A中的记录 还应记录表B中的记录 所以我的结果应该
- 表A有列
id | name | age
- 表B有列
id | name | age
1|xavi |23
2|christine|24
3|faisal |25
5|jude |27
表B中的样本记录
1|xavi |23
2|christine|22
3|faisal |23
4|ram |25
如果表A中的id值与表B中的匹配,则仅从表A中获取记录。
同时,只记录表A中的记录
还应记录表B中的记录
所以我的结果应该是
1|xavi |23
2|christine|24
3|faisal |25
4|ram |25
5|jude |27
<>一些要考虑的事情-->除非你正在更新特定的表和记录是相同的,你从哪个表中查看记录是无关紧要的(因为它们是相同的……)。
如果您想查看记录是从哪个表派生的,请告诉我,我也将向您演示如何执行此操作。。。但是查询更复杂,我真的认为上面描述的目的不需要它。让我知道这是否有帮助。。。谢谢,Brian您可以简单地使用union运算符从两个表中获取唯一的值。运算符联合将删除重复的值
SELECT * FROM tableA AS t1
UNION
SELECT * FROM tableB AS t2
如果表与您需要的关系密切:
Select DISTINCT *
from tableA a
Inner Join tableB b
On a.id = b.id
如果没有:
在使用UNION和DISTINCT之后,必须使用UNION。
DISTINCT将不允许重复行。此处存在优先级问题。从表A中取出所有记录,然后从表B中取出额外记录:
select *
from A
union all
select *
from B
where B.id not in (select A.id from A);
您还可以使用完全外部联接
来表示这一点(假设id
在两个表中都不重复):
在这种情况下,coalesce()
优先考虑A
中的值
select *
from A
union all
select *
from B
where B.id not in (select A.id from A);
select coalesce(A.id, B.id) as id,
coalesce(A.name, B.name) as name,
coalesce(A.age, B.age) as age
from A full outer join
B
on A.id = B.id;