-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                                              |
+-----------------------------------------------------+
| "[email protected]" |
| "[email protected]"                       |
| "[email protected]"                      |
| "[email protected]"                          |
| "[email protected]"                     |
| "[email protected]"                          |
| "[email protected]"                         |
| "[email protected]"                   |
| "[email protected]"                        |
| "[email protected]"                 |
+-----------------------------------------------------+
10 rows in set (0.00 sec)

Blocks

mysql> select * from blocks limit 10;    
+---------------------------------------------------+
| email                                             |
+---------------------------------------------------+
| ""                                                |
| [email protected] |
| [email protected]                                |
| [email protected]                                |
| [email protected]                                |
| [email protected]                     |
| [email protected] |
| [email protected] |
| [email protected] |
| [email protected] |
+---------------------------------------------------+
10 rows in set (0.00 sec)

Expected result from Sent table

+-----------------------------------------------------+
| emails                                              |
+-----------------------------------------------------+
| "[email protected]" |
| "[email protected]"                      |
| "[email protected]"                          |
| "[email protected]"                     |
| "[email protected]"                          |
| "[email protected]"                         |
| "[email protected]"                   |
| "[email protected]"                        |
| "[email protected]"                 |
+-----------------------------------------------------+

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. [email protected] 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