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,
The updates are processed sequentially in MySql, so the update to
num_unmarkedoccurs before theIFstatement, and theIFstatement 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: