と の 2 つのテーブルがtable1
ありtable2
ます。両方のテーブルに同じ列があります。
レコードの違いを抽出したい - つまり、 not in でレコードをtable1
抽出し、 not inでtable2
レコードを抽出します。table2
table1
Oracle SysでSQLを使用してそれを行うにはどうすればよいですか?
と の 2 つのテーブルがtable1
ありtable2
ます。両方のテーブルに同じ列があります。
レコードの違いを抽出したい - つまり、 not in でレコードをtable1
抽出し、 not inでtable2
レコードを抽出します。table2
table1
Oracle SysでSQLを使用してそれを行うにはどうすればよいですか?
使用できる1つの結果セットで両方のテーブルのすべての違いを取得するには
select columnlist
from tableA
minus
select columnlist
from tableB
union all
select columnlist
from tableB
minus
select columnlist
from tableA
select t1.column1,t1.column2 from table1 t1 except select t2.column1,t2.column2 from table 2 t2 UNION all select t2.column1,t2.column2 from table 2 t2 except select t1.column1,t1.column2 from table1 t1
select t1.Id,t1.name,t2.Id,t2.name from table1 t1 ,table2 t2 where t1.Id and t1.Name not in(select * from table2) and t2.Id,t2.name not in (select * from table 2)
どのテーブルに実際に行が含まれているかに関する追加情報を含む、RomanKonz のソリューション (私は通常、実際の UNION ALL / MINUS 部分を単純化するために WITH を使用します):
with
v_a as (
select *
from tableA),
v_b as (
select *
from tableB)
select * from
(
(select 'A only' src, v.* from v_a v
minus
select 'A only' src, v.* from v_b v
)
union all
(select 'B only' src, v.* from v_b v
minus
select 'B only' src, v.* from v_b v
)
) order by primarykeycolumn, src
MINUS 1 つの SELECT ステートメントの結果セットを取得し、2 番目の SELECT ステートメントによっても返される行を削除します。
このクエリは、table1 にあるが table2 にはないすべての行を返します。
SELECT * FROM table1
MINUS
SELECT * FROM table2;
select * from table1 where pk_col not in(select pk_col from table2)
select * from table2 where pk_col not in(select pk_col from table1)