Reputation: 7
Here is my query:
SELECT *
FROM client_info
WHERE (cid LIKE 'G%' OR cid LIKE 'H%' OR cid LIKE 'J%')
AND (client_name NOT LIKE 'P4%' OR client_name NOT LIKE 'P5%')
The results still contain client names starting with P4 and P5. What is wrong with NOT LIKE clause?
Upvotes: 0
Views: 90
Reputation: 40336
Others have given you the correct answer - you need to use AND instead of OR. But it's worth understanding why. Take a client named P5_JONES, for instance - why is this patient showing up in the list? Because its name is NOT LIKE 'P4%'. Yes, it is like P5%, but with an OR in there, only one of those expressions needs to be true to satisfy the condition.
Upvotes: 1
Reputation: 15379
Try this:
SELECT * FROM client_info WHERE (cid LIKE 'G%' OR cid LIKE 'H%' OR cid LIKE 'J%') AND (client_name NOT LIKE 'P4%' AND client_name NOT LIKE 'P5%')
You must use AND and not OR operator. Because you use NOT LIKE so, you must check either condition.
Upvotes: 0
Reputation: 34055
Change the second set's OR
to an AND
.
AND (client_name NOT LIKE 'P4%' AND client_name NOT LIKE 'P5%')
Upvotes: 3