如何从一个表中选择不匹配的行到另一个表?

时间:2017-08-24 12:55:40

标签: mysql sql innodb

Mysql v5.7 我知道这个问题之前已经被问到并得到了解答,但是一般的查询在我的数据库中无法正常工作而且我的头发都没有了。 我有两个表只有一列包含电子邮件:1)已发送(41110行[有重复])2)块(81132 [有重复])

mysql> show create table blocks;
+--------+------------------------------------------------------------------------------------------------+
| Table  | Create Table                                                                                   |
+--------+------------------------------------------------------------------------------------------------+
| blocks | CREATE TABLE `blocks` (
  `email` varchar(200) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+--------+------------------------------------------------------------------------------------------------+

mysql> show create table sent;
+-------+-----------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                  |
+-------+-----------------------------------------------------------------------------------------------+
| sent  | CREATE TABLE `sent` (
  `emails` varchar(111) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------------------------------------------------------+

我想通过“已发送” - “阻止”或获取行 我想从“已发送”表中获取那些不在“块”表

中的行

已发送的表格

mysql> select * from sent limit 10;
+-----------------------------------------------------+
| emails                                              |
+-----------------------------------------------------+
| "52@mail.marketplace" |
| "1cstains@coden.com"                       |
| "a.aldhizer@ldplastics.com"                      |
| "a.antosca@tdx.com"                          |
| "a.balvanz@hrrd-paper.com"                     |
| "a.bandal@medpace.com"                          |
| "a.bass@paettethanol.com"                         |
| "a.bodwalk@bedfndustries.com"                   |
| "a.bravo@sdfhemie.com"                        |
| "a.burton@evdarenterprises.com"                 |
+-----------------------------------------------------+
10 rows in set (0.00 sec)

阻止

mysql> select * from blocks limit 10;    
+---------------------------------------------------+
| email                                             |
+---------------------------------------------------+
| ""                                                |
| 0f6a88be0a4b45628be38ae08c8fdd71@mail.marketplace |
| 1cstains@coden.com                                |
| 1cstains@coden.com                                |
| 1cstains@coden.com                                |
| 3161foodmgr@mcare.com                     |
| 4b00fce87e5b423c942f5b19f27c3a13@mail.marketplace |
| 52d05d98b59e44b0816401d2cd0411f0@mail.marketplace |
| 52d05d98b59e44b0816401d2cd0411f0@mail.marketplace |
| 6357a2fd35114418a93e0ccda6edd6f4@mail.marketplace |
+---------------------------------------------------+
10 rows in set (0.00 sec)

已发送表格的预期结果

+-----------------------------------------------------+
| emails                                              |
+-----------------------------------------------------+
| "52@mail.marketplace" |
| "a.aldhizer@ldplastics.com"                      |
| "a.antosca@tdx.com"                          |
| "a.balvanz@hrrd-paper.com"                     |
| "a.bandal@medpace.com"                          |
| "a.bass@paettethanol.com"                         |
| "a.bodwalk@bedfndustries.com"                   |
| "a.bravo@sdfhemie.com"                        |
| "a.burton@evdarenterprises.com"                 |
+-----------------------------------------------------+

这是我使用的查询,它返回匹配的行:/

select sent.emails from sent where sent.emails NOT IN ( select email from blocks )

2 个答案:

答案 0 :(得分:0)

select sent.emails 
from sent 
     left join blocks on sent.emails = blocks.email
where blocks.email is null

如果问题是电子邮件中的"字符(如提到@Jens),您可以更改连接以使用子字符串剪切"字符以跳过第一个和最后一个字符

left join blocks on SUBSTRING(sent.emails,1,len(sent.emails)-2) = blocks.email

答案 1 :(得分:-1)

请使用minus运营商。

select sent.emails from sent 
minus
select blocks.email from blocks