我有这张表:
// QandA
+----+-----------+-----------------+-------------+------+------+
| id | post | accepted-answer | question_id | let | type |
+----+-----------+-----------------+-------------+------+------+
| 1 | question1 | null | 1 | 200 | 0 |
| 2 | answer1 | null | 1 | null | 1 |
| 3 | answer2 | 1 | 1 | null | 1 |
| 4 | answer3 | null | 1 | null | 1 |
+----+-----------+-----------------+-------------+------+------+
// ^ 0 = question | 1 = answer
我还有这个疑问:
// this query changes accepted answer
UPDATE QandA
SET accepted_answer = IF(id <> :id, NULL, 1)
WHERE question_id = :question_id;
<小时/>
现在我尝试在更新之前检查 let
字段。像这样的事情:
if (`let` is null) then {update here}
else {don't update}
where `question_id` = :question_id and `type` = 0
如何在 MySQL 中做到这一点?
最佳答案
您可以使用case语句,下面的语句将更新QandA表。
当let为null并且类型为0时,它会将accepted_answer设置为1。
尝试一下,我知道它适用于 ms sql,只是目前无法访问我的 sql 框。
UPDATE QandA SET accepted_answer =
CASE
WHEN let = IS NULL THEN 1
END
WHERE type= 0;
关于mysql - 更新前如何检查记录的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36755769/