I am writing a MySQL query where I can get all records from the user table with duplicate email addresses.
This is the query I have so far.. my PHPMyAdmin keeps loading:
select personal_email, userid
from user
where personal_email in (
select personal_email
from user
group by personal_email
having count(personal_email) > 1);
If I use the query below I get all double emails just once:
select personal_email, count(personal_email)
from user
group by personal_email
having count(personal_email) > 1
The goal is getting all the records with duplicate emails.
Mailchimp automatically scans for duplicates when you add or import contacts to a single audience. If an address is found in an import file twice or more, we'll only add it once. If you try to add someone who's already in your audience, we'll prevent it to avoid duplication.
Duplicates of the same message will occur if your email account is configured to forward email to multiple addresses. For example, the original may arrive in your business acount with a copy forwarded to your home account. They will both arrive in the same inbox if the same mail client is checking both addresses.
Try the query with a JOIN instead of IN
:
SELECT user.personal_email, user.userid
FROM user
INNER JOIN
( SELECT personal_email
FROM User
GROUP BY personal_email
HAVING COUNT(*) > 1
) dupe
ON dupe.personal_email = user.personal_email;
MySQL often optimises INNER JOINs much better.
Perhaps the most efficient way to do this in MySQL is to use an exists
clause:
select u.*
from user u
where exists (select 1
from user u2
where u.personal_email = u2.personal_email and
u.userid <> u2.userid
);
For optimal performance, create an index on user(personal_email, userid)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With