之前有人问过这不完全符合我的需要。这是我想要做的。我正在使用Oracle SQL。我有两个表,有一些不相交和一些匹配的元素。如果MID元素匹配(第一列),我希望它们合并表-B应该取代的条件。
CREATE TABLE TABLE_A ( MID VARCHAR2(255), DESCRIPTION VARCHAR2(300) );
CREATE TABLE TABLE_B ( MID VARCHAR2(255), DESCRIPTION VARCHAR2(300) );
-- Table A
INSERT INTO TABLE_A VALUES ( 'M-003', 'ABC03' );
INSERT INTO TABLE_A VALUES ( 'M-004', 'ABC04' );
INSERT INTO TABLE_A VALUES ( 'M-005', 'ABC05' );
INSERT INTO TABLE_A VALUES ( 'M-006', 'ABC06' );
INSERT INTO TABLE_A VALUES ( 'M-007', 'ABC07' );
INSERT INTO TABLE_A VALUES ( 'M-008', 'ABC08' );
-- Table B
INSERT INTO TABLE_B VALUES ( 'M-001', 'ABC01' );
INSERT INTO TABLE_B VALUES ( 'M-002', 'ABC02' );
INSERT INTO TABLE_B VALUES ( 'M-003', 'ABC03' );
INSERT INTO TABLE_B VALUES ( 'M-004', 'DEF04' );
INSERT INTO TABLE_B VALUES ( 'M-009', 'ABC09' );
INSERT INTO TABLE_B VALUES ( 'M-010', 'ABC10' );
表-A
M-003 ABC03
M-004 ABC04
M-005 ABC05
M-006 ABC06
M-007 ABC07
M-008 ABC08
表-B
M-001 ABC01
M-002 ABC02
M-003 ABC03
M-004 DEF04
M-009 ABC09
M-010 ABC10
期望的结果:
M-001 ABC01
M-002 ABC02
M-003 ABC03
M-004 DEF04
M-005 ABC05
M-006 ABC06
M-007 ABC07
M-008 ABC08
M-009 ABC09
M-010 ABC10
以下没有工作;
SELECT A.MID, A.DESCRIPTION
FROM TABLE_A A, TABLE_B B
WHERE TABLE_A.MID != TABLE_B.MID
UNION
SELECT * FROM TABLE_B;
如何使用Oracle SQL实现此目的?
答案 0 :(得分:3)
通过使用FULL OUTER JOIN
和COALESCE
Select Coalesce(B.MID, A.MID) As MID,
Coalesce(B.DESCRIPTION, A.DESCRIPTION) As DESCRIPTION
From Table_A A
Full Outer Join Table_B B On A.MID = B.MID
;