[英]How to Insert / Update a Row in MySQL / PHP At The Same Time
I want to insert into a MySQL row if the row doesn't exist, or update it if it does exist.如果该行不存在,我想插入到 MySQL 行中,或者如果它存在则更新它。
Something like this:像这样:
$sqlQuery = "INSERT INTO table (Value1, Value2) VALUES ('$var1', '$var2') WHERE UniqueKey='$id'";
Is something like that possible?这样的事情可能吗?
INSERT INTO table (UniqueKey,Value1, Value2) VALUES ('$id','$var1', '$var2')
ON DUPLICATE KEY UPDATE Value1 = '$var1',Value2 = '$var1';
User MySQL's REPLACE command:用户 MySQL 的REPLACE命令:
$sqlQuery = "REPLACE INTO table (id, Value1, Value2) VALUES ('$id', '$var1', '$var2')";
It works the same as a normal INSERT, but if the primary key (in you case 'id') matches then it will replace all the values specified.它的工作方式与普通 INSERT 相同,但如果主键(在您的情况下为“id”)匹配,那么它将替换所有指定的值。
replace into table (UniqueKey,Value1,Value2) values ('$id','$var1','$var2');替换为表 (UniqueKey,Value1,Value2) 值 ('$id','$var1','$var2');
replace is similar to insert, however if the unique key exists in table, it will delete first, and than insert. replace 类似于insert,但是如果表中存在唯一键,它会先删除,然后再插入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.