2017-07-28 103 views
0

我想两个表结合垂直结合使用合并两个表

表一

ID Salary       
A  50        
B  100 

表B中

ID Salary 
C  50 
D  200 

我试图得到一个表,看起来像

ID Salary 
A  50      
B  100 
C  50 
D  200 

我正在使用这个:

merge into table_a a using (
    select id, salary from table B 
) b 
on (a.id = b.id); 

这是行不通的。

+1

第一件事:这不是PL/SQL。第二件事:XING指出,合并声明缺少更多选项。第三件事:你只是想查询一下,或者确实要合并数据?如果您想查询,请使用联合运算符,如SMA所写。 –

+0

从表B'看起来不对。这应该是从table_b'吗? (加上缺少其他人提到的'insert' /'update'部分。) –

回答

1

Merge语法是不正确的。见下文。了解更多关于合并Here

MERGE INTO Table_a a 
    USING (SELECT Id, Salary FROM TABLE B) b 
ON a.id = b.id 
when not matched then 
insert 
(id,salary) 
values 
(b.id,b.salary); 
0

使用联盟或联合所有

SELECT ID,工资FROM表A

UNION

SELECT ID,工资表B FROM

0

我在这里实现了一些步骤,你可以按照他们每个人的

第1步:创建2代表“表-A”和“表-B”

create table table_a(ID varchar2(10),salary number(10)); 

create table table_b(ID varchar2(10),salary number(10)); 

步骤2:填充它们的数据:

insert into table_a(id,salary) values ('A',50); 
insert into table_a(id,salary) values ('B',100); 

insert into table_b(id,salary) values ('C',50); 
insert into table_b(id,salary) values ('D',200); 

第3步:合并声明在这里,请小心ul你必须使用语句“当匹配时”

merge into table_a a 
using table_b b 
on (a.id = b.id) 
when matched then 
    update set a.salary = b.salary 
when not matched then 
    insert (id, salary) values (b.id, b.salary);