如何只在一个MySQL查询中执行以下两个更新?
$query = "UPDATE news SET main = 1 WHERE id = '$id'";
mysqli_query($this->db_conn, $query);
$query = "UPDATE news SET main = 0 WHERE id <> '$id'";
mysqli_query($this->db_conn, $query);
只应将一行设置为1
,而其余行应设置为0
。
答案 0 :(得分:3)
尝试一下:
UPDATE news SET main = CASE
WHEN id = '$id' THEN 1
ELSE 0
END