IntoTheDeep
IntoTheDeep

Reputation: 4118

Query MySQL database find missing email addresses

I am trying to extract email addresses from given list, that not persists in MySql database. My query:

SELECT * 
FROM users 
WHERE `user_email` IN ('[email protected]', '[email protected]', '[email protected]')

First two email addresses are in database, but the last one is not. My target is to print only emails that are NOT in database. How is that possible?

Upvotes: 0

Views: 342

Answers (2)

Moulitharan M
Moulitharan M

Reputation: 749

create a temporary table and insert all the email ids that you want to check

create table check_emailid(email_id varchar(255))

insert into check_emailid(email_id )
values('[email protected]') 
values('[email protected]')
values('[email protected]')

select * 
from check_emailid 
where email_id not in (SELECT user_email
                       FROM users)

Upvotes: 0

Akina
Akina

Reputation: 42632

SELECT * 
FROM (SELECT '[email protected]' user_email
      UNION ALL
      SELECT '[email protected]'
      UNION ALL
      SELECT '[email protected]') emails_to_check_for
LEFT JOIN users USING (user_email)
WHERE users.user_email IS NULL;

Upvotes: 2

Related Questions