mysql条件插入 - 如果不存在则插入

时间:2021-11-27 22:24:11

Is there a query that will just check for the record and if it doesn't exists insert? I don't want to on duplicate update or replace. Looking for a one query solution, looked at other answer but not really what I was hoping for.

是否有查询只检查记录,如果它不存在插入?我不想重复更新或替换。寻找一个查询解决方案,看看其他答案,但不是我真正希望的。

Table:

表:

name|value|id
------------------
phill|person|12345

pseudo query:

伪查询:

IF NOT EXISTS(name='phill', value='person', id=12345) INSERT INTO table_name

2 个解决方案

#1


18  

Use REPLACE - works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.

使用REPLACE - 与INSERT完全相同,只是如果表中的旧行与PRIMARY KEY或UNIQUE索引的新行具有相同的值,则在插入新行之前删除旧行。

http://dev.mysql.com/doc/refman/5.0/en/replace.html

http://dev.mysql.com/doc/refman/5.0/en/replace.html

-- For your example query
REPLACE INTO table_name(name, value, id) VALUES
('phill', 'person', 12345) 

Edit: Since you can't use REPLACE another option is to: set constraint indexes for the table data (primary key, uniqueness) and use INSERT IGNORE

编辑:由于您不能使用REPLACE,另一个选项是:为表数据设置约束索引(主键,唯一性)并使用INSERT IGNORE

INSERT IGNORE INTO table_name
SET name = 'phill',
    value = 'person',
    id = 12345;

#2


2  

How about doing an insert from a select query which has a sub-query to only return a row if the row does not already exist?

如果从具有子查询的select查询执行插入,如果该行尚不存在,则仅返回行?

Pseudocode:

伪代码:

INSERT INTO TABLE_FOO (...)
SELECT @VAR1, VAR2 ...
FROM DUMMYTABLE -- THIS TABLE SHOULD CONTAIN 1 RECORD OF ANYTHING
                -- IN CASE THE FROM IS REQUIRED
WHERE (SELECT COUNT(*) FROM TABLE_FOO WHERE COLUMN1 = @VAR1) = 0

This pattern should work, you'll just need to get the syntax correct for MySQL.

这个模式应该可行,你只需要让MySQL的语法正确。

#1


18  

Use REPLACE - works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.

使用REPLACE - 与INSERT完全相同,只是如果表中的旧行与PRIMARY KEY或UNIQUE索引的新行具有相同的值,则在插入新行之前删除旧行。

http://dev.mysql.com/doc/refman/5.0/en/replace.html

http://dev.mysql.com/doc/refman/5.0/en/replace.html

-- For your example query
REPLACE INTO table_name(name, value, id) VALUES
('phill', 'person', 12345) 

Edit: Since you can't use REPLACE another option is to: set constraint indexes for the table data (primary key, uniqueness) and use INSERT IGNORE

编辑:由于您不能使用REPLACE,另一个选项是:为表数据设置约束索引(主键,唯一性)并使用INSERT IGNORE

INSERT IGNORE INTO table_name
SET name = 'phill',
    value = 'person',
    id = 12345;

#2


2  

How about doing an insert from a select query which has a sub-query to only return a row if the row does not already exist?

如果从具有子查询的select查询执行插入,如果该行尚不存在,则仅返回行?

Pseudocode:

伪代码:

INSERT INTO TABLE_FOO (...)
SELECT @VAR1, VAR2 ...
FROM DUMMYTABLE -- THIS TABLE SHOULD CONTAIN 1 RECORD OF ANYTHING
                -- IN CASE THE FROM IS REQUIRED
WHERE (SELECT COUNT(*) FROM TABLE_FOO WHERE COLUMN1 = @VAR1) = 0

This pattern should work, you'll just need to get the syntax correct for MySQL.

这个模式应该可行,你只需要让MySQL的语法正确。