mysql insert on duplicate key

Posted Go_Forward

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql insert on duplicate key相关的知识,希望对你有一定的参考价值。

mysql 插入数据,如果存在,更新

INSERT INTO subs
  (subs_name, subs_email, subs_birthday)
VALUES
  (?, ?, ?)
ON DUPLICATE KEY UPDATE
  subs_name     = VALUES(subs_name),
  subs_birthday = VALUES(subs_birthday)

如果批量操作,使用python:

sql = "insert into elec_workshop_15min_2017(start_time, "               "name " \"values(%(start_time)s, %(name)s) ""on DUPLICATE KEY UPDATE stat_time = values(start_time), "               "name = values(name)"

mysql_conn.executemany(sql, data) # data is a list of dict, for method to insert batch of tuple, google it.

ref: https://stackoverflow.com/questions/15383852/sql-if-exists-update-else-insert-into



以上是关于mysql insert on duplicate key的主要内容,如果未能解决你的问题,请参考以下文章

MySQL--INSERT INTO ... ON DUPLICATE KEY UPDATE ...

mysql INSERT ... ON DUPLICATE KEY UPDATE语句

mysql INSERT ... ON DUPLICATE KEY UPDATE语句

Java上的MySQL“INSERT ... ON DUPLICATE KEY UPDATE”:如何区分插入/更新/无变化状态

MySQL中的`REPLACE`和`INSERT ... ON DUPLICATE KEY UPDATE`之间有啥实际区别?

MySQL INSERT 与 ON DUPLICATE SELECT 可能吗?