我正在向数据库中插入搜索词,当我运行它进行测试时,我看到重复项被插入到数据库中。我能够解决这个问题,但是,现在我正在插入,它不再作为副本插入,但我正在尝试让on副本工作-并且on副本每次都会将流行度更新2?这里我有什么问题吗?
$entryDate = date("c");
$insertsearchquery="insert into article_searches (termSafe,entryDate) values (\"$termSafe\",\"$entryDate\") on duplicate key update popularity=popularity+1";
mysql_query($insertsearchquery);我为termSafe和entryDate设置了一个唯一的密钥集。
TABLE `article_searches` (
`id` bigint(20) NOT NULL AUTO INCREMENT ,
`termSafe` varchar(150) ,
`entryDate` varchar(255),
`popularity` tinyint(6) UNSIGNED NOT NULL,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `termSafe` USING BTREE (`termSafe`),
UNIQUE INDEX `entryDate` USING BTREE (`entryDate`)
)虽然我刚刚删除了entryDate唯一索引。
发布于 2014-08-12 05:37:16
我猜这是因为你有一个唯一的(termSafe)和唯一的( entryDate),也许你只是想要一个唯一的(termSafe,entryDate)。
请记住,mysql_*函数已弃用。您应该改用PDO或mysqli_*。
发布于 2014-08-12 05:38:20
尝试删除两个唯一索引,只创建一个:
create unique index idx_article_searches_termSafe_EntryDate on article_searches(termSafe, EntryDate);使用on duplciate key update时,当检测到多个唯一约束冲突时,行为可能定义不明确。我认为它做了两次更新,每个约束一次。
Here是文档中的警告:
一般来说,您应该尽量避免对具有多个唯一索引的表使用ON DUPLICATE UPDATE子句。
https://stackoverflow.com/questions/25252785
复制相似问题