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 |
+------+--------+
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;
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'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.