[英]MySQL NOT IN or NOT REGEXP
I have 2 tables: voters
, banned_domains
I want to get all the votes except where banned domains This query works well 我有2个表:
voters
, banned_domains
我想获得所有选票,除非被禁止的域名这个查询运作良好
SELECT * FROM `voters` email NOT REGEXP('tempmail.com|tempmail.org')
I want to compare with the request to another table, but this code does not work: 我想与另一个表的请求进行比较,但此代码不起作用:
SELECT * FROM `voters` email NOT IN(SELECT domain FROM banned_domains)
Thanks a lot 非常感谢
UPDATE : I apologize for the incomplete data. 更新 :我为不完整的数据道歉。 In the
voters
I have a full email address example 'oleole@mail.com' but in the banned_domains
i have only domain names example 'mail.com' 在
voters
我有一个完整的电子邮件地址示例'oleole@mail.com'但在banned_domains
我只有域名示例'mail.com'
I changed the query, but it does not work 我更改了查询,但它不起作用
SELECT * FROM voters
WHERE DISTINCT RIGHT( voters.email , LENGTH( voters.email ) - LOCATE( '@', voters.email ) ) NOT IN (SELECT domain FROM banned_domains)
ERROR:#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT RIGHT( voters.email , LENGTH( voters.email ) - LOCATE( '@', voters.e' at line 2
You are missing where
in your query change 你缺少
where
在查询变化
SELECT * FROM `voters` email NOT IN(SELECT domain FROM banned_domains)
to 至
SELECT * FROM `voters` WHERE email NOT IN(SELECT domain FROM banned_domains)
You need to perform a REGEXP
match between the email and domain, but NOT IN
performs an exact match. 您需要在电子邮件和域之间执行
REGEXP
匹配,但NOT IN
执行完全匹配。
SELECT v.*
FROM voters AS v
LEFT JOIN banned_domains AS d ON v.email REGEXP d.domain
WHERE d.domain IS NULL
A typical reason for this would be that domain
takes on NULL
values. 一个典型的原因是
domain
采用NULL
值。 You can fix this by explicitly filtering these out: 你可以通过显式过滤这些来解决这个问题:
SELECT *
FROM `voters`
WHERE email NOT IN (SELECT domain FROM banned_domains WHERE domain is not null);
However, I prefer using NOT EXISTS
, because the semantics are more what people expect: 但是,我更喜欢使用
NOT EXISTS
,因为语义更符合人们的期望:
SELECT v.*
FROM `voters` v
WHERE NOT EXISTS (SELECT 1 FROM banned_domains bd WHERE bd.domain = v.domain);
NOTE: I assume the issue is not the missing where
clause, because you claim the first query works. 注意:我认为问题不是缺少
where
子句,因为您声称第一个查询有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.