我很难理解如何做到这一点。commentid
是comment
表中某一行的id。vote
表有三列:id
, commentid
&username
。
我想要的是插入到vote
表只有当最后两列(注释和用户名)不存在。$commentids
是由comment
表中的多个$id
组成的数组。我如何添加唯一索引来实现这一点?
$commentids = explode(".",$id);
foreach($commentids as $value) {
$insert = mysql_query("INSERT IGNORE INTO vote
(id, commentid, username)
VALUES
('','$value','$username')", $this->connect);
}
如果您希望(commentid,username)
对是唯一的,则:
alter table vote add constraint unique (commentid, username);
如果您希望commentid
和username
在表中都是全局唯一的,那么:
alter table vote add constraint unique (commentid);
alter table vote add constraint unique (username);