query including an IF statement
I want my query to using a IF statement, identify; if the current num_unmarked
will become 0 afte开发者_如何学Gor 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,
The updates are processed sequentially in MySql, so the update to num_unmarked
occurs before the IF
statement, and the IF
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:
is_final = IF(num_unmarked=0, 'YES', is_final),
精彩评论