牧羊人nacy
第一步是在表上设置一个唯一的键:ALTER TABLE thetable ADD UNIQUE INDEX(pageid, name);当有重复的时候,你必须决定你想做什么。你应该:无视它?INSERT IGNORE INTO thetable (pageid, name) VALUES (1, "foo"), (1, "foo");覆盖以前输入的记录?INSERT INTO thetable (pageid, name, somefield)VALUES (1, "foo", "first")ON DUPLICATE KEY UPDATE (somefield = 'first')
INSERT INTO thetable (pageid, name, somefield)VALUES (1, "foo", "second")ON DUPLICATE KEY UPDATE (somefield = 'second')更新一些柜台?INSERT INTO thetable (pageid, name)VALUES (1, "foo"), (1, "foo")ON DUPLICATE KEY UPDATE (pagecount = pagecount + 1)