正确的做法是在表上定义一个唯一索引(这相当于一个约束):
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
);