-1

Mysql v5.7 I know this question is asked and answered before but the general queries is not working in my database and im pulling my hair off. I have two tables that only have one column containing emails : 1) Sent (41110 rows [there are duplicates]) 2) Blocks (81132 [there are duplicates])

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 |
+-------+-----------------------------------------------------------------------------------------------+

I want to get rows as a result of "Sent" - "Blocks" OR I want to get those rows from "Sent" table that is not in "Blocks" table

Sent table:

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)

Blocks

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)

Expected result from Sent table

+-----------------------------------------------------+
| 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"                 |
+-----------------------------------------------------+

Here is the query i used and it returns matching rows :/

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

sherpaurgen
  • 3,028
  • 6
  • 32
  • 45

2 Answers2

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

if the problem is " char in emails (as @Jens was mentioned) you can change join to cut the " char using subbstring to skip first and last chars

left join blocks on SUBSTRING(sent.emails,1,len(sent.emails)-2) = blocks.email
StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • You can also use `replace` function to remove the double quotes, it is safer if there are emails without double quotes too – Jens Aug 24 '17 at 13:32
  • @Jens you are right. I consider `Replace` could be dangerous when string contains `"` internally. Not remember whether `"` is allowed in emails though – StanislavL Aug 24 '17 at 13:38
  • I do not think double quotes are valid characters in email addresses – Jens Aug 24 '17 at 13:39
  • @Jens https://stackoverflow.com/questions/2049502/what-characters-are-allowed-in-an-email-address looks like `"` is allowed. - **e.g. John..Doe@example.com is not allowed but "John..Doe"@example.com is allowed** – StanislavL Aug 24 '17 at 13:44
  • Yes i have seen it in the same Moment on Wikipedia. But both versions of the SQL are risky without analyseing the data. – Jens Aug 24 '17 at 13:45
-1

Please use minus Operator.

select sent.emails from sent 
minus
select blocks.email from blocks
SQL.RK
  • 157
  • 4