我有一个代码,如果它不存在,则在数据库中创建具有指定 id 的行,否则更新该行。它有点慢(它检查是否存在一行,然后插入/更新它),我想优化它。如何将整个事情整合到一个 SQL 查询中?我想我应该使用 INSERT ... ON DUPLICATE KEY UPDATE 但我不明白它是如何工作的。谢谢你。
$objectExists = true;
try {
$object = new Object();
$object->loadFromDatabaseById($this->id, $dbh); // tries to load object from database, throws
// ExceptionNotFound if object with specified id does not exist
}
catch(ExceptionNotFound $e) {
$objectExists = false;
}
if($objectExists)
$sth = $dbh->prepare(
'UPDATE objects ' .
'SET property1 = :property1, ' .
'property2 = :property2, ' .
'property3 = :property3 ' .
'WHERE id = :id'
);
else
$sth = $dbh->prepare(
'INSERT INTO objects ' .
'SET id = :id, ' .
'property1 = :property1, ' .
'property2 = :property2, ' .
'property3 = :property3'
);
$sth->bindParam(':id', $this->id, \PDO::PARAM_INT);
$sth->bindParam(':property1', $this->property1, \PDO::PARAM_STR);
$sth->bindParam(':property2', $this->property2, \PDO::PARAM_STR);
$sth->bindParam(':property3', $this->property3, \PDO::PARAM_INT);
$sth->execute();
$this->id = (int)$this->id;