第一步是在表上设置唯一键:
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)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)