Stack Stack - 5 months ago 9
MySQL Question

How can I make a condition opposite in MySQL?

I have this query:

$db
->prepare("UPDATE users
SET reputation = reputation +
CASE
WHEN id = ? THEN 2
WHEN id = ? AND ? THEN 15
END
WHERE id IN (?, ?); ")
->execute(array($author_ques_id, $author_ans_id, $bool, $author_ans_id, $author_ques_id));


Please focus on this condition:

WHEN id = ? AND ? THEN 15
// ^ this is containing a boolean value


And I want to opposite the value of that condition. I mean I want to run that condition when
$bool
is
false
and don't run it when it is
true
.

I can do that before passing by php:

$bool = !$bool;


But I want to know can I do that in the query by MySQL?

Answer

Use NOT:

... AND NOT ? THEN 15
Comments