表结构:
CREATE TABLE `test` (
`int` int(11) NOT NULL AUTO_INCREMENT,
`str` varchar(45) DEFAULT NULL,
thekey` varchar(45) NOT NULL,
PRIMARY KEY (`int`),
UNIQUE KEY `thekey_UNIQUE` (`thekey`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8
也就是说thekey是UNIQUE KEY!!
INSERT INTO test (str,thekey )
VALUES (‘111′,’key’)
ON DUPLICATE KEY UPDATE str=’111-3′, thekey=’key-2-3′ ;
插入时,如果存在thekey和要插入的值相同’key’的记录时,将执行更新UPDATE str=’111-3′, thekey=’key-2-3′ ;