简体   繁体   English

MySQL INSERT,如果存在UPDATE

[英]MySQL INSERT else if exists UPDATE

I'm building a config table with two columns, config_name and config_value. 我正在建立一个包含两列的配置表:config_name和config_value。 I insert multiple rows in one statement: 我在一条语句中插入多行:

INSERT INTO ".$dbPrefix."config (config_name,config_value) VALUES
             ('domain','$domain'),
             ('forest_root','$fr_if'),
             ('userGroup','$userGroup'),
             ('adminGroup','$adminGroup');

The config_name column is a primary key. config_name列是主键。 How would I change this statement to automatically update the config_value if the config_name already exists? 如果config_name已经存在,我将如何更改此语句以自动更新config_value?

You could try this syntax: 您可以尝试使用以下语法:

INSERT INTO table (field) VALUES (value) ON DUPLICATE KEY UPDATE field=value

Docs can be found here . 可以在这里找到文档。

您正在尝试进行upsert,但我认为这可能会有所帮助: http : //database-programmer.blogspot.com/2009/06/approaches-to-upsert.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM