如何通过一个查询更新两行?

时间:2016-04-20 16:08:40

标签: mysql sql sql-update

我有一张这样的表:

// Answers
+----+---------+-----------------+-------------+
| id |  answer | accepted-answer | question_id |
+----+---------+-----------------+-------------+
| 1  | answer1 | null            | 1           |
| 2  | answer2 | 1               | 1           |
| 3  | answer3 | null            | 1           |
+----+---------+-----------------+-------------+

现在我正在尝试更改accepted-answer。我想要输出:

+----+---------+-----------------+-------------+
| id |  answer | accepted-answer | question_id |
+----+---------+-----------------+-------------+
| 1  | answer1 | null            | 1           |
| 2  | answer2 | null            | 1           |
| 3  | answer3 | 1               | 1           |
+----+---------+-----------------+-------------+

注意:我只有新的ID (新接受的答案的ID,而不是旧答案),在上面的示例中为$id=3。我在上面的示例中也有question_id $question_id=1

我可以通过以下两个独立的查询来实现:

UPDATE Answers SET accepted_answer = null WHERE question_id = :question_id;

UPDATE Answers SET accepted_answer = 1 WHERE id = :id;

现在我想知道,我能在同一时间和一次查询中做到这一点吗?

0 个答案:

没有答案