SQL语句实现不存在即插入,存在则increase某字段的功能insert into … on duplicate key update

前提条件:必须是唯一主键;

CREATE UNIQUE INDEX idx_vote_object ON test_customers_vote (`vote_object`, `vote_object_id`);

用一条sql实现“不存在即插入,存在则increase 某字段”的复合功能 – insert into … on duplicate key update
比如:
CREATE UNIQUE INDEX comp ON logs (`site_id`, `time`);

INSERT INTO logs (`site_id`, `time`,`hits`) VALUES (1,"2004-08-09", 15) ON DUPLICATE KEY UPDATE hits=hits+15;

文章来源:http://hi.baidu.com/caoxin_rain/item/7b7139e99f4763e6fb42ba60

上一篇:ubuntu 14.04为/检查磁盘时发生严重错误的解决方法


下一篇:mysql 插入更新在一条sql ON DUPLICATE KEY UPDATE