Skip to content
Advertisement

Do you know how to include and exclude parts of a string in mysql where clause?

I am having trouble using and/or/not operators in my where clause to create the following logic.

I have a field called package that lists out many different names of packages and I need to include/exclude specifically based on the following rules:

  1. If a package includes renew, optout, or discount in the name AND ALSO does not include premium in the name, those names need to be excluded from the dataset. (so If a package has renew, optout, or discount in the name and the word premium, these are included in the dataset)

  2. Furthermore, Anything with employee or Comp needs to be excluded.

  3. Anything else not listed in the rules above would be included in the dataset.

Any help would be greatly appreciated and added to my sql tool belt!

Advertisement

Answer

Sounds like something like:

SELECT *
FROM packages
WHERE name NOT LIKE '%employee%'
  AND name NOT LIKE '%Comp%'
  AND (name NOT LIKE '%renew%' OR name NOT LIKE '%optout%' OR name NOT LIKE '%discount%') OR name LIKE '%premium%')
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement