I have this query , where the results are as expected but the query is really slow. The below is just an example
SELECT ispending, isviewable, iscomparable, ID
FROM tableA
WHERE
name = 'Karen'
AND NOT ((ispending = 'F' AND isviewable = '0') OR
(ispending = 'T' AND iscomparable = '0') OR
(ispending = 'T' AND iscomparable IS NULL AND isviewable = '0') OR
(ispending IS NULL AND iscomparable = '0'))
How to achieve the same result but not using the 'NOT' statement in the where clause?
I tried changing the not to be within the clause
WHERE (ispending != 'F' AND isviewable != '0') OR
(ispending != 'T' AND iscomparable != '0') OR
(ispending != 'T' AND iscomparable IS NOT NULL AND isviewable != '0') OR
(ispending IS NOT NULL AND iscomparable !='0')
but the expected results are different.
CodePudding user response:
You're second code block is quite close. De Morgan's law guides in trying to switch operands on boolean operations.
The code is correct to switch =
with !=
(the negation of the =
). But you will also need to negate conjunctions/disjunctions. In essence: AND
becomes OR
and vice versa.
WHERE (ispending != 'F' OR isviewable != '0') AND
(ispending != 'T' OR iscomparable != '0') AND
(ispending != 'T' OR iscomparable IS NOT NULL OR isviewable != '0') AND
(ispending IS NOT NULL OR iscomparable != '0')
Now we have the logical equivalent.