我如何在mysql中插入多个值并避免重复 [英] how do i insert multiple values in mysql and avoid duplicates
问题描述
表格是
id,subject1,subject2,subject3
id是auto增加。
一个副本将是所有subject1,subject2,subject3已经以完全相同的顺序存在于记录中的地方。
code> INSERT INTOtable_name(subject1,subject2,subject3)
VALUES(cats,dogs,hamsters)
VALUES(squirrels ,獾,minxes)
VALUES(moose,deer,ocelots)
在表格中,我已经有了
id,subject1,subject2,subject3 $ b的记录$ b 1,猫,狗,仓鼠
所以我想要它插入
VALUES(squirrels,badgers,minxes)
VALUES(moose鹿,ocelots)
您要添加 UNIQUE
UNIQUE
约束,则更清楚如何将其应用于列的任意组合。 CREATE TABLE table_name(
subject1 VARCHAR(30),
subject2 VARCHAR(30),
subject3 VARCHAR(30),
UNIQUE(subject1 ,subject2,subject3)
);
How would I insert multiple rows or values and avoid duplicates in the following schema.
table schema is
id,subject1,subject2,subject3
id is auto incremented.
A duplicate would be where all subject1,subject2,subject3 already exist in a record in the exact same order.
INSERT INTO "table_name" ("subject1","subject2","subject3")
VALUES ("cats", "dogs", "hamsters")
VALUES ("squirrels", "badgers", "minxes")
VALUES ("moose", "deer", "ocelots")
In the table let's say I already have a record for
id,subject1,subject2,subject3
1,"cats", "dogs", "hamsters"
so I want it to just insert
VALUES ("squirrels", "badgers", "minxes")
VALUES ("moose", "deer", "ocelots")
I've seen answers about avoiding duplicates for single items, but not for 3.
You want to add the UNIQUE
constraint to your table. If you write the UNIQUE
constraint out separately, it becomes clearer how to apply it to arbitrary combinations of columns.
CREATE TABLE table_name (
subject1 VARCHAR(30),
subject2 VARCHAR(30),
subject3 VARCHAR(30),
UNIQUE (subject1, subject2, subject3)
);
这篇关于我如何在mysql中插入多个值并避免重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!