MySQLは存在する行のみを返します。欠落している行を戻すには、2つの表が必要です。
最初のテーブルは、複数のインスタンスを同時に実行できるように、一時的(セッション/接続固有)にすることができます。
create temporary table tmpMustExist (text id);
insert into tmpMustExist select "ida";
insert into tmpMustExist select "idb";
-- etc
select a.id from tmpMustExist as a
left join table b on b.id=a.id
where b.id is null; -- returns results from a table that are missing from b table.
が、これは、単一のSQLクエリで可能ですか?
まあまあです。最初にunion all
select
ステートメントを組み合わせると私の方法を働かせてください。それは重複排除をスキップしているためunion
よりも少し速いです私はunion all
を使用
create temporary table tmpMustExist (text id);
insert into tmpMustExist select "ida" union all select "idb" union all select "etc...";
select a.id from tmpMustExist as a left join table as b on b.id=a.id where b.id is null;
注意。
create table
... select
を使用できます。私はこれを頻繁に行い、本当にそれが好きです。 (それだけでなく、テーブルをコピーするのに最適な方法ですが、それはインデックスを削除します。)
create temporary table tmpMustExist as select "ida" union all select "idb" union all select "etc...";
select a.id from tmpMustExist as a left join table as b on b.id=a.id where b.id is null;
そして最後に、あなたは、単一の、ポータブルの選択に全体のことをもたらすために、「派生」テーブルと呼ばれるものを使用することができますステートメント。
select a.id from (select "ida" union all select "idb" union all select "etc...") as a left join table as b on b.id=a.id where b.id is null;
注:as
キーワードはオプションですが、私はa
とb
でやって明確にしています。私は単純なトリックがあります
これはアプリケーションコードで簡単に行うことができます。どのようなプログラミング言語を使用していますか? – tadman
'' id ''、 'idb'、 'idc') ' –
のようなサウンド' SELECT a.id FROM input_set a LEFT JOINテーブルb ON a.id = b.id WHERE b.id IS NULL' – Serg