Reputation: 788
In the below table, I need to exclude CategoryID=1002
and Gender='KIDS'
record from the table and return other records.
+------+--------+
| ID | Gender |
+------+--------+
| 1001 | MENS |
| 1002 | MENS |
| 1001 | WOMENS |
| 1002 | WOMENS |
| 1001 | KIDS |
| 1002 | KIDS |
+------+--------+
Expected results:
+------+--------+
| ID | Gender |
+------+--------+
| 1001 | MENS |
| 1002 | MENS |
| 1001 | WOMENS |
| 1002 | WOMENS |
| 1001 | KIDS |
+------+--------+
Upvotes: 0
Views: 68
Reputation: 1269763
I don't recommend using case
expressions in the where
clause, because that can affect the optimizer.
This is easily expressed as boolean logic:
where not (CategoryID = 1002 and Gender = 'KIDS')
or:
where CategoryID <> 1002 or Gender <> 'KIDS'
Upvotes: 1
Reputation: 14928
You could use a CASE
expression as
SELECT *
FROM
(
VALUES
(1001, 'MENS'),
(1002, 'MENS'),
(1001, 'WOMENS'),
(1002, 'WOMENS'),
(1001, 'KIDS'),
(1002, 'KIDS')
) T(Id, Gender)
WHERE CASE WHEN ID = 1002 AND Gender = 'KIDS' THEN 1 ELSE 0 END = 0;
Upvotes: 2