一个MySQL查询中的两个更新

时间:2016-02-26 17:43:08

标签: mysql updates

如何只在一个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

1 个答案:

答案 0 :(得分:3)

尝试一下:

UPDATE news SET main = CASE
    WHEN id = '$id' THEN 1
    ELSE 0
END