仅当列为空时更新 SQLite 行
Update SQLite row only if a column is empty
我有以下内容:
$query = $db->prepare("UPDATE name SET one=?,two=?,three=? WHERE id = ?");
$query->bindParam(1, $one);
$query->bindParam(2, $two);
$query->bindParam(3, $three);
$query->bindParam(4, $id);
one
和 three
应始终更新。
但是,如果是 null/empty,我如何才能更新 two
?
我可以在单独的命令中执行此操作,但如何在一个准备好的语句中实现此操作?
我正在使用 SQLite3
对第 two
列使用 CASE
表达式:
UPDATE name
SET one=?,
two=CASE WHEN two IS NULL THEN ? ELSE two END,
three=?
WHERE id = ?
或 COALESCE()
:
UPDATE name
SET one=?,
two=COALESCE(two, ?),
three=?
WHERE id = ?
用例
UPDATE name
SET one=?,two=case when two is null or two = '' then ? else two end, three=?
WHERE id = ?
我有以下内容:
$query = $db->prepare("UPDATE name SET one=?,two=?,three=? WHERE id = ?");
$query->bindParam(1, $one);
$query->bindParam(2, $two);
$query->bindParam(3, $three);
$query->bindParam(4, $id);
one
和 three
应始终更新。
但是,如果是 null/empty,我如何才能更新 two
?
我可以在单独的命令中执行此操作,但如何在一个准备好的语句中实现此操作?
我正在使用 SQLite3
对第 two
列使用 CASE
表达式:
UPDATE name
SET one=?,
two=CASE WHEN two IS NULL THEN ? ELSE two END,
three=?
WHERE id = ?
或 COALESCE()
:
UPDATE name
SET one=?,
two=COALESCE(two, ?),
three=?
WHERE id = ?
用例
UPDATE name
SET one=?,two=case when two is null or two = '' then ? else two end, three=?
WHERE id = ?