2016-05-11 10 views
0

、table1.col1はtable2.col2存在しないリターンTABLE1のレコードを見つけ、私が試してみたい(ただし、失敗した)SQL:これは私の文</p> <pre><code>select * from table1 where col1 not in (select col2 from table2); </code></pre> <p>ある

select t1.* from table1 t1 
inner join table2 on t1.col1<> t2.col2 

行うための別の方法はありますこの?

+0

サブクエリの期待される結果は何ですか?nullを返しますか? (つまり、table2がnullのcol2値を持つ場合)。 – jarlh

答えて

0
select t1.* from table1 t1 
left join table2 t2 on t1.col1 = t2.col2 
where t2.col2 is null 

これは、レコードの検索を行方不明の標準的な実装です。

0

t2オブジェクトを定義していないため、left joinを使用してください。

SELECT t1.* FROM table1 t1 
    LEFT JOIN table2 t2 ON t2.col2 = t1.col1 WHERE t2.id IS NULL 
0

table1に存在するがtable2に存在しないレコードを取得するには、inner joinではなくleft joinが必要です。

これを試してみてください:

select t1.* 
from table1 t1 
left join table2 t2 on t1.col1 = t2.col2 
where t2.id is null -- any non-nullable column will do, best to use pk for performance. 
0

あなたは、スクリプト

を流れる使用T2.C2に存在して存在するかしないT1.C1(T1 = TABLE1、C1 = COLUMN1)のデータを見つけるためにしようとした場合

select t1.c1 from t1 left join t2 on t1.c1=t2.c2

あなたは、スクリプト以下T2.C2ではないだけT1.C1データを検索する場合は賢明な他の助けでフル

select t1.c1 from t1 where t1.c1 not in (select t2.c2 from t2)

関連する問題