2017-07-28 96 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);