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

posted @ 2014-04-04 14:48  培杰  阅读(450)  评论(0编辑  收藏  举报