简体   繁体   English

创建一个mysql记录(如果不存在),否则更新它

[英]create a mysql record if it doesnt exist, else update it

I would like to modify this MySQL query to insert the row only if it does not exist. 我想修改此MySQL查询以仅在该行不存在时才插入该行。 Otherwise, I would like to update the existing row. 否则,我想更新现有的行。

<?php
$query = mysql_query("UPDATE hqfjt_email_history SET datesent = ['datesent'],emailformname = ['emailformname'],leadname = ['leadname']
WHERE emailformname = ['emailformname'] AND leadname = ['leadname']") or die(mysql_error());
?>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ EDIT >> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~编辑>>

Would this be correct ? 这是正确的吗?

INSERT INTO hqfjt_email_history datesent = ['datesent'],emailformname =     ['emailformname'],leadname = ['leadname'] ON DUPLICATE KEY UPDATE hqfjt_email_history SET     datesent = ['datesent'],emailformname = ['emailformname'],leadname = ['leadname']
WHERE emailformname = ['emailformname'] AND leadname = ['leadname']

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~

I have tried the above and get the error : You have an error in your SQL syntax; 我已经尝试了上面的方法并得到了错误:您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'datesent = ['datesent'],emailformname = ['emailformname'],leadname = ['leadname'' at line 1 检查与您的MySQL服务器版本相对应的手册,以在第1行的'datesent = ['datesent'],emailformname = ['emailformname'],leadname = ['leadname'附近使用正确的语法

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~

UPDATE ! 更新! Ok, I tried this and it creates the record ok : 好的,我尝试了这个,它创建了ok的记录:

If I do this though, nothing happens, nothing gets updated or created ???? 如果我这样做,什么也没发生,什么也没有更新或创建? :

<?php
$query = mysql_query("
INSERT INTO hqfjt_email_history (id, leadname, emailformname, datesent)
VALUES
('$_POST[id]','$_POST[leadname]','$_POST[emailformname]','$_POST[datesent]') ON         DUPLICATE KEY UPDATE hqfjt_email_history (datesent) VALUES ('$_POST[datesent]')     WHERE id = '$_POST[id]'");
?>

Yes, use the INSERT INTO ... ON DUPLICATE KEY UPDATE syntax. 是的,使用INSERT INTO ... ON DUPLICATE KEY UPDATE语法。

http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

You will need to have a key defined on the table of types UNIQUE or PRIMARY KEY . 您将需要在类型UNIQUEPRIMARY KEY的表上定义一个键。

There is also REPLACE . 也有REPLACE

Which will update the existing row or will create a new one if it doesn't exist, also the syntax is similar to INSERT . 它将更新现有行,或者如果不存在则创建一个新行,语法类似于INSERT

It does exactly what INSERT ... ON DUPLICATE KEY UPDATE does, but I think this approach is more elegant. 它的功能与INSERT ... ON DUPLICATE KEY UPDATE功能完全相同,但是我认为这种方法更为优雅。

Yeah, use ON DUPLICATE KEY UPDATE 是的,使用ON DUPLICATE KEY UPDATE

for that. 为了那个原因。 Here is an example: 这是一个例子:

INSERT INTO <table> (<field1>, <field2>) VALUES (<value1>, <value2>)
ON DUPLICATE KEY UPDATE <field2> = 'update CONTENT';

important is the primary key (underlined in phpMyAdmin table). 重要的是主键(在phpMyAdmin表中加下划线)。

read this url 阅读这个网址

http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

check the database emailformname and leadname are unique 检查数据库emailformname和Leadname是否唯一

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

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