这是我需要的一个例子:
表1:
Id champ1 champ2 champ3
1 A S Q
2 D K A
3 D A S
表2:
Id champ1 champ2 champ3
1 A S Q
2 Q A A
3 A A K
作为表1和表2之间进行比较的结果,提供table3的过程
表3:
Id_exc name_champ noted_val except_val
2 champ1 D Q
3 champ1 D A
2 champ2 K A
3 champ3 S K
如果有人遇到同样的挑战,我需要帮助:)
先谢谢
答案 0 :(得分:1)
数据设置:
CREATE TABLE Table1
(Id int, champ1 varchar2(1), champ2 varchar2(1), champ3 varchar2(1))
;
INSERT ALL
INTO Table1 (Id, champ1, champ2, champ3)
VALUES (1, 'A', 'S', 'Q')
INTO Table1 (Id, champ1, champ2, champ3)
VALUES (2, 'D', 'K', 'A')
INTO Table1 (Id, champ1, champ2, champ3)
VALUES (3, 'D', 'A', 'S')
SELECT * FROM dual
;
CREATE TABLE Table2
(Id int, champ1 varchar2(1), champ2 varchar2(1), champ3 varchar2(1))
;
INSERT ALL
INTO Table2 (Id, champ1, champ2, champ3)
VALUES (1, 'A', 'S', 'Q')
INTO Table2 (Id, champ1, champ2, champ3)
VALUES (2, 'Q', 'A', 'A')
INTO Table2 (Id, champ1, champ2, champ3)
VALUES (3, 'A', 'A', 'K')
SELECT * FROM dual
;
以下查询会为您提供差异
SELECT *
FROM ((SELECT *
FROM table1
minus
SELECT *
FROM table2)
UNION
(SELECT *
FROM table2
minus
SELECT *
FROM table1));
ID CHAMP1 CHAMP2 CHAMP3
2 D K A
2 Q A A
3 A A K
3 D A S
答案 1 :(得分:1)
这是一个获得结果的查询,虽然它很难看:
SELECT
t1.id AS Id_exc, 'champ1' AS name_champ, t1.champ1 AS noted_val, t2.champ1 AS except_val
FROM
Table1 t1 INNER JOIN Table2 t2 ON t2.id = t1.id WHERE t1.champ1 <> t2.champ1
UNION
SELECT
t1.id AS Id_exc, 'champ2' AS name_champ, t1.champ2 AS noted_val, t2.champ2 AS except_val
FROM
Table1 t1 INNER JOIN Table2 t2 ON t2.id = t1.id WHERE t1.champ2 <> t2.champ2
UNION
SELECT
t1.id AS Id_exc, 'champ3' AS name_champ, t1.champ3 AS noted_val, t2.champ3 AS except_val
FROM
Table1 t1 INNER JOIN Table2 t2 ON t2.id = t1.id WHERE t1.champ3 <> t2.champ3
答案 2 :(得分:0)
创建OR替换程序P_test是
q varchar2(4000字节);
V_SELECT VARCHAR2(2000):= NULL;
开始
用于录入(从user_tab_columns中选择COLUMN_NAME,其中table_name ='TABLE1'和column_name&lt;&gt;'ID_T')循环
V_SELECT:= NULL;
V_SELECT:= V_SELECT || rec.COLUMN_NAME;
q:='SELECT t1.id_t AS Id,'''|| V_SELECT ||'''AS nom_champ,t1。'|| V_SELECT ||'如上所述,t2。'|| V_SELECT ||'除了'||
'FROM TABLE1 t1 INNER JOIN TABLE2 t2 ON t1.id_t = t2.id_t WHERE t1。'|| V_SELECT ||' &LT;&GT; 。T2'|| V_SELECT;
q:='INSERT INTO TABLE3'|| Q;
dbms_output.put_line('查询:'|| q);
立即执行q;
结束循环; 端;