0

準備されたステートメントを使用して、製品をtableusingに挿入します。INSERT INTO DBXY.table (category, title, description, cost, supplier) VALUES (?,?,?,?,?)

supplierは数字です。

テーブルには、同じカテゴリ、タイトル、説明、コスト、およびサプライヤーを持つエントリが既に含まれている場合があります。どのエントリが挿入されるか、つまり、重複していないエントリを知りたいです。私はできる

SELECT * FROM table WHERE (category = PHPcat) AND (title = PHPtitle) AND....

各エントリに対して、結果の行数が 0 より大きい場合、重複があり、新しいエントリはありません。しかし、このアプローチは 6000 以上のエントリには欠陥があると思います。今すぐ挿入せずにすべての新しいエントリを含む出力を取得するために、これを一度に行う方法はありますか?

4

1 に答える 1

2

これを行う正しい方法は、テーブルに一意のインデックスを定義することです (これは制約と同等です)。

create unique index table_allcols on table(category, title, description, cost, supplier);

同様にこれを行うこともできinsertます:

INSERT INTO DBXY.table (category, title, description, cost, supplier)
    select category, title, description, cost, supplier
    from (select ? as category,? as title, ? as description, ? as cost, ? as supplier) t
    where not exists (select 1
                      from table t2
                      where t2.category = table.category and
                            t2.title = table.title and
                            t2.description = table.description and
                            t2.cost = table.cost and
                            t2.supplier = table.supplier
                     );

編集:

一致するリストを見つけるには、一時テーブルを作成してそれらを結合します。

select tt.*
from temporary_table tt
where not exists (select 1
                  from table t2
                  where t2.category = tt.category and
                        t2.title = tt.title and
                        t2.description = tt.description and
                        t2.cost = tt.cost and
                        t2.supplier = tt.supplier
                 );
于 2013-08-23T02:15:11.383 に答える