简体   繁体   English

如何对多个值使用LIKE运算符

[英]How to use LIKE operator against multiple values

I have this query : 我有这个查询:

select * from users where mailaddress  
  NOT like '%banned_domain1.com%'  
    AND mailaddress NOT like '%banned_domain2.com%'  
      AND mailaddress NOT like '%banned_domain3.com%' ;

I want to make it more simple , I executed this query from command line : 我想使其更简单,我从命令行执行了以下查询:

select * from users where mailaddress NOT like ('%banned_domain1.com%','%banned_domain2.com%','%banned_domain3.com%') ;

I got MySQL error : 我收到MySQL错误:

ERROR 1241 (21000): Operand should contain 1 column(s)

You can use NOT REGEXP 您可以使用NOT REGEXP

SELECT * FROM users WHERE mailaddress NOT REGEXP 'banned_domain1.com|banned_domain2.com|banned_domain3.com';

See live demo 观看现场演示

Instead of "Like" use "In" and format the email address like this: 代替“ Like”,使用“ In”并设置电子邮件地址的格式,如下所示:

select * from users where SUBSTR(mailaddress, INSTR(mailaddress, '@') + 1)
NOT IN ('banned_domain1.com','banned_domain2.com','banned_domain3.com');

The SUBSTR will remove the @ and anything preceding it, leaving only the domain name then you can do a perfect comparison without wildcards using IN. SUBSTR将删除@及其后的所有内容,仅保留域名,然后您可以使用IN进行通配符的完美比较。

Cheers! 干杯!

you have to mention the column every time 每次都要提一下专栏

select * from tasks where title NOT LIKE '%eating lunch%' AND title NOT LIKE '%eating breakfast%' AND title NOT LIKE '%a new task%'

however as Bruno Domingues said use NOT IN that will be more easy 但是,正如Bruno Domingues所说,使用NOT IN会更容易

You cannot simplify your query. 您无法简化查询。 You need one LIKE per condition. 每个条件您需要一个LIKE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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