插入多个值并忽略非关键字段上的重复项
Insert multiple values and ignore duplicates on not key field
如果我想将多个值插入 table 字段:
ID | name | content
---+------+------------------
1 | john | some content
2 | bob | another content
现在我想插入多个值:
INSERT INTO persons (name,content)
VALUES
("Foo","meh"),
("Moo","beh"),
("bob","huh"),
("dude","haha")
结果应该是:
ID | name | content
---+------+------------------
1 | john | some content
2 | bob | another content
3 | Foo | meh
4 | Moo | beh
5 | dude | haha
查询忽略名称重复项,因此未插入 "bob"|"huh",名称不是关键字段。我在任何地方都找不到它的语法,我知道它应该很简单。
我正在使用 MySQL 数据库。
这是在 tsql 中,但您可以使用合并语句创建一个存储过程,该语句具有 table 类型(名称和内容作为列)作为参数以在插入前检查名称。如果名称匹配,则不会插入,但如果不匹配,则会插入一条新记录。
BEGIN TRANSACTION
-- setup tables for merge
MERGE dbo.persons AS t
USING @NameContentTable AS s
ON t.name = s.name
-- do nothing if matched
-- insert if not matched
WHEN NOT MATCHED THEN
INSERT (name,
content)
VALUES (s.name,
s.content);
-- error handling
IF @@ERROR <> 0
BEGIN
SELECT 'Unexpected error occurred'
ROLLBACK TRAN
RETURN -1
END
COMMIT TRANSACTION
使用LEFT JOIN
判断记录是否已经在table
CREATE TABLE persons(ID INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
content VARCHAR(100));
INSERT INTO persons (name, content)
VALUES ('john', 'some content'), ('bob', 'another content');
INSERT INTO persons (name,content)
SELECT t.name, t.content
FROM
(
SELECT "Foo" AS name,"meh" AS content
UNION ALL
SELECT "Moo","beh"
UNION ALL
SELECT "bob","huh"
UNION ALL
SELECT "dude","haha"
) AS t
LEFT JOIN persons p
ON p.name = t.name
WHERE p.id IS NULL;
SELECT *
FROM persons;
同样使用NOT EXISTS
INSERT INTO persons (name,content)
SELECT t.name, t.content
FROM
(
SELECT "Foo" AS name,"meh" AS content
UNION ALL
SELECT "Moo","beh"
UNION ALL
SELECT "bob","huh"
UNION ALL
SELECT "dude","haha"
) AS t
WHERE NOT EXISTS
(SELECT 1 FROM persons p WHERE p.name = t.name)
编辑:
添加UNIQUE KEY
并使用INSERT IGNORE INTO
CREATE TABLE persons(ID INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) UNIQUE KEY,
content VARCHAR(100));
INSERT INTO persons (name, content)
VALUES ('john', 'some content'), ('bob', 'another content');
INSERT IGNORE INTO persons (name,content)
VALUES
("Foo","meh"),
("Moo","beh"),
("bob","huh"),
("dude","haha");
SELECT *
FROM persons;
如果我想将多个值插入 table 字段:
ID | name | content
---+------+------------------
1 | john | some content
2 | bob | another content
现在我想插入多个值:
INSERT INTO persons (name,content)
VALUES
("Foo","meh"),
("Moo","beh"),
("bob","huh"),
("dude","haha")
结果应该是:
ID | name | content
---+------+------------------
1 | john | some content
2 | bob | another content
3 | Foo | meh
4 | Moo | beh
5 | dude | haha
查询忽略名称重复项,因此未插入 "bob"|"huh",名称不是关键字段。我在任何地方都找不到它的语法,我知道它应该很简单。
我正在使用 MySQL 数据库。
这是在 tsql 中,但您可以使用合并语句创建一个存储过程,该语句具有 table 类型(名称和内容作为列)作为参数以在插入前检查名称。如果名称匹配,则不会插入,但如果不匹配,则会插入一条新记录。
BEGIN TRANSACTION
-- setup tables for merge
MERGE dbo.persons AS t
USING @NameContentTable AS s
ON t.name = s.name
-- do nothing if matched
-- insert if not matched
WHEN NOT MATCHED THEN
INSERT (name,
content)
VALUES (s.name,
s.content);
-- error handling
IF @@ERROR <> 0
BEGIN
SELECT 'Unexpected error occurred'
ROLLBACK TRAN
RETURN -1
END
COMMIT TRANSACTION
使用LEFT JOIN
判断记录是否已经在table
CREATE TABLE persons(ID INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
content VARCHAR(100));
INSERT INTO persons (name, content)
VALUES ('john', 'some content'), ('bob', 'another content');
INSERT INTO persons (name,content)
SELECT t.name, t.content
FROM
(
SELECT "Foo" AS name,"meh" AS content
UNION ALL
SELECT "Moo","beh"
UNION ALL
SELECT "bob","huh"
UNION ALL
SELECT "dude","haha"
) AS t
LEFT JOIN persons p
ON p.name = t.name
WHERE p.id IS NULL;
SELECT *
FROM persons;
同样使用NOT EXISTS
INSERT INTO persons (name,content)
SELECT t.name, t.content
FROM
(
SELECT "Foo" AS name,"meh" AS content
UNION ALL
SELECT "Moo","beh"
UNION ALL
SELECT "bob","huh"
UNION ALL
SELECT "dude","haha"
) AS t
WHERE NOT EXISTS
(SELECT 1 FROM persons p WHERE p.name = t.name)
编辑:
添加UNIQUE KEY
并使用INSERT IGNORE INTO
CREATE TABLE persons(ID INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) UNIQUE KEY,
content VARCHAR(100));
INSERT INTO persons (name, content)
VALUES ('john', 'some content'), ('bob', 'another content');
INSERT IGNORE INTO persons (name,content)
VALUES
("Foo","meh"),
("Moo","beh"),
("bob","huh"),
("dude","haha");
SELECT *
FROM persons;