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