php 如果条目不存在则创建,否则更新?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/696190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:33:11  来源:igfitidea点击:

Create if an entry doesn't exist, otherwise update?

phpmysql

提问by unrelativity

Kinda strange to put it into words that short, heh.

用这么短的话来说有点奇怪,呵呵。

Anyway, what I want is basically to update an entry in a table if it does exist, otherwise to create a new one filling it with the same data.

无论如何,我想要的基本上是更新表中的条目(如果它确实存在),否则创建一个用相同数据填充它的新条目。

I know that's easy, but I'm relatively new to MySQL in terms of how much I've used it :P

我知道这很容易,但就我使用它的程度而言,我对 MySQL 还比较陌生:P

采纳答案by carl

Use 'REPLACE INTO':

使用“替换为”:

 REPLACE INTO table SET id = 42, foo = 'bar';

See more in the MySQL documentation

MySQL 文档中查看更多信息

回答by Kennethvr

A lot of developers still execute a query to check if a field is present in a table and then execute an insert or update query according to the result of the first query. Try using the ON DUPLICATE KEY syntax, this is a lot faster and better then executing 2 queries. More info can be found here

许多开发人员仍然执行查询以检查表中是否存在字段,然后根据第一个查询的结果执行插入或更新查询。尝试使用 ON DUPLICATE KEY 语法,这比执行 2 个查询更快更好。更多信息可以在这里找到

INSERT INTO table (a,b,c) VALUES (4,5,6) ON DUPLICATE KEY UPDATE c=9;

INSERT INTO table (a,b,c) VALUES (4,5,6) ON DUPLICATE KEY UPDATE c=9;

if you want to keep the same value for c you can do an update with the same value

如果您想为 c 保持相同的值,您可以使用相同的值进行更新

INSERT INTO table (a,b,c) VALUES (4,5,6) ON DUPLICATE KEY UPDATE c=6;

INSERT INTO table (a,b,c) VALUES (4,5,6) ON DUPLICATE KEY UPDATE c=6;

the difference between 'replace' and 'on duplicate key':

“替换”和“重复键”之间的区别:

replace: inserts, or deletes and inserts

on duplicate key: inserts or updates

替换:插入,或删除并插入

在重复键上:插入或更新

if your table doesn't have a primary key or unique key, the replace doesn't make any sense.

如果您的表没有主键或唯一键,则替换没有任何意义。

You can also use the VALUESfunction to avoid having to specify the actual values twice. E.g. instead of

您还可以使用该VALUES函数来避免两次指定实际值。例如代替

INSERT INTO table (a,b,c) VALUES (4,5,6) ON DUPLICATE KEY UPDATE c=6;

INSERT INTO table (a,b,c) VALUES (4,5,6) ON DUPLICATE KEY UPDATE c=6;

you can use

您可以使用

INSERT INTO table (a,b,c) VALUES (4,5,6) ON DUPLICATE KEY UPDATE c=VALUES(c);

INSERT INTO table (a,b,c) VALUES (4,5,6) ON DUPLICATE KEY UPDATE c=VALUES(c);

Where VALUES(c)will evaluate to the value specified prevously (6).

WhereVALUES(c)将评估为先前指定的值 (6)。

回答by nickf

As the others have said, REPLACEis the way to go. Just be careful using it though, since it actually does a DELETEand INSERTon the table. This is fine most of the time, but if you have foreign keys with constraints like ON DELETE CASCADE, it can cause some big problems.

正如其他人所说,这REPLACE是要走的路。不过要小心使用它,因为它实际上在桌子上做了一个DELETEINSERT。大多数情况下这很好,但是如果您有带有约束的外键,例如ON DELETE CASCADE,它可能会导致一些大问题。

回答by Vegard Larsen

Look up REPLACEin the MySQL manual.

在 MySQL 手册中查找REPLACE

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. See Section 12.2.5, “INSERT Syntax”.

REPLACE is a MySQL extension to the SQL standard. It either inserts, or deletes and inserts. For another MySQL extension to standard SQL — that either inserts or updates — see Section 12.2.5.3, “INSERT ... ON DUPLICATE KEY UPDATE Syntax”.

REPLACE 的工作方式与 INSERT 完全相同,但如果表中的旧行与 PRIMARY KEY 或 UNIQUE 索引的新行具有相同的值,则在插入新行之前删除旧行。见第 12.2.5 节,“插入语法”。

REPLACE 是 SQL 标准的 MySQL 扩展。它要么插入,要么删除和插入。对于标准 SQL 的另一个 MySQL 扩展——插入或更新——请参阅 第 12.2.5.3 节,“INSERT ... ON DUPLICATE KEY UPDATE Syntax”。

If you have the following INSERT query:

如果您有以下 INSERT 查询:

INSERT INTO table (id, field1, field2) VALUES (1, 23, 24)

This is the REPLACE query you should run:

这是您应该运行的 REPLACE 查询:

REPLACE INTO table (id, field1, field2) VALUES (1, 23, 24)