MySQL on duplicate key update

蹲街弑〆低调 提交于 2019-12-03 13:59:18
Joshua Martell

This should work and is a little more elegant:

INSERT INTO TABLENAME(col1, col2) 
VALUES (’xxx’, ‘yyy’) 
ON DUPLICATE KEY UPDATE col1 = VALUES(col1)

Note that you don't need to update the primary key part of the row. We know that's the same because there was a collision.

Marc Armstrong

In PHP I do this to solve the issue:

$fields = "a = 1, b = 2, c = 3";
$sql = "INSERT INTO some_table SET id = $id, $fields ON DUPLICATE KEY UPDATE $fields";
mysql_query($sql);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!