我正在使用一个非常棒的代码片段,在重复键上插入AND,它会更新重复的id并获取最后一个插入ID,无论是否重复。因此,我得到最后更新或重复ID的ID。但有没有办法区分这两者?
$prepare=$connection->prepare("
insert into category(related,text) values(?,?)
on duplicate key update id=last_insert_id(id)");
$prepare->execute([1,"property"]);
print_r($connection->lastinsertid());
// this is both the update id and duplicate update id
无论是否出现重复错误,都会给我一个ID。但我想区分id是否重复。类似的东西:
$prepare=$connection->prepare("
insert into category(related,text) values(?,?)
on duplicate key select id");
$prepare->execute([1,"property"]);
$fetch=$prepare->fetch();
if(!empty($fetch)){
echo 'DUPLICATE';
$id=$fetch['id'];
// this is the duplicate update id
}
else{
$id=$connection->lastinsertid();
// this is the update id
}