Delete Duplicate Emails

Source: Internet
Author: User
Tags mysql delete

    Write a SQL query to delete all duplicate e-mail entries in a table named person, keeping unique emails based on I    TS smallest Id. +----+------------------+    | Id |    Email | +----+------------------+    | 1 |    [Email protected] | | 2 |    [Email protected] | | 3 |    [Email protected] |    +----+------------------+ Id is the primary key, column for this table.     For example, after running your query, the above person table should has the following rows: +----+------------------+ | Id |    Email | +----+------------------+    | 1 |    [Email protected] | | 2 |    [Email protected] | +----+------------------+ Find First, and then delete the delete from person where (id,email) not in (select Id,email from person GROUP by E    Mail order by Email); ERROR 1093 (HY000): You can ' t specify the target table ' person ' for the update in FROM clause means: You cannot query the delete condition in a table and then delete the update in this list    。 Workaround: Create a temporary table, then find the delete condition, and finally delete the update select * from the person P1 INNER JOIN person P2 WHERE p1. Email = P2. Email and P1. Id > P2.    Id; +------+---------+------+---------+    | ID | Email | ID |    Email |    +------+---------+------+---------+    | 3 |    [Email protected] | 1 |    [Email protected] |    | 6 |    [Email protected] | 1 |    [Email protected] |    | 4 |    [Email protected] | 2 |    [Email protected] |    | 6 |    [Email protected] | 3 |    [Email protected] | +------+---------+------+---------+ delete p1 from person p1 inner join person p2 where P1.email = P2.email and P1.id & Gt    P2.id;    or delete P1 from P1 using the person P1 inner The join person p2 where P1.email = P2.email and p1.id > p2.id; [MySQL Delete multiple table association Delete] (http://blog.csdn.net/havedream_one/article/details/45421017)

Delete Duplicate Emails

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.