简体   繁体   中英

How to exclude a list of results from mysql query

Lets say I want to select all rows that does not contain a long list of words. Lets say this list is shortened for the sake of this example to only "example1" and "example2".

Im guessing using REGEXP would be the best option, instead of specifying

and field not like '%example1%' and field not like '%example2%'

but Im not sure how to go about it. Im guessing something like this?

WHERE !REGEXP(field, '/example1|example2/')

MySQL support for regular expressions is very limited, but I think you could use this solution:

SELECT *
FROM yourtable
WHERE
  field NOT RLIKE '[[:<:]]example1[[:>:]]|[[:<:]]example2[[:>:]]'

where [[:<:]] and [[:>:]] are word boundaries, and | is the OR operator

I would use LOCATE:

set @string = '/example1|example2/';
select * from YourTable
where locate(YourStringField,@String) = 0

http://www.sqlfiddle.com/#!2/6d075/3

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM