包含 IF 语句的查询
我希望我的查询使用 IF 语句,identify;如果当前的 num_unmarked
在此更新后将变为 0,则还将 is Final
设置为“YES”。
所以这是我的查询:
$sql = "UPDATE results_tb
SET
num_unmarked = num_unmarked - 1,
is_final=IF( num_unmarked-1=0, 'YES', is_final ),
score = score + $awarded
WHERE
user_id = $student
AND
test_id = $test
";
实际发生的情况是,查询运行,num_unmarked 递减并且分数更新,但是即使 num_unmarked 为 1(不是 0),它仍然将 is_final 设置为“YES”。
有人可以告诉我我做错了什么或者启发我更好的方法吗? (我是菜鸟:D)
谢谢,
I want my query to using a IF statement, identify; if the current num_unmarked
will become 0 after this update, then also set is final
to 'YES'.
so here is my query:
$sql = "UPDATE results_tb
SET
num_unmarked = num_unmarked - 1,
is_final=IF( num_unmarked-1=0, 'YES', is_final ),
score = score + $awarded
WHERE
user_id = $student
AND
test_id = $test
";
what is actually happening is, the query runs, num_unmarked decrements and score is updated however even if num_unmarked is 1 (not 0) it still sets is_final to 'YES'.
could somebody tell me what I am doing wrong please or enlighten me to a better way? (i am nooby :D)
thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新在 MySql 中按顺序处理,因此对
num_unmarked
的更新发生在IF
语句之前,并且IF
语句使用新更新的值。因此,您实际上想要使用该值,就像它已经被更新一样,即直接将其与 0 进行比较:The updates are processed sequentially in MySql, so the update to
num_unmarked
occurs before theIF
statement, and theIF
statement uses the newly updated value. So you actually want to use the value as if it has already been updated, i.e., compare it directly to 0: