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