title :
Write a SQL query to delete all duplicate e-mail entries in a table named Person
, keeping unique emails based on its 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] | +----+------------------+
Ideas:
Write SQL to delete all duplicate email entries in the person table, leaving only the only email record with the smallest ID.
Where the ID is the primary key of the table.
Solution One:
# Write Your MySQL query statement below DELETE from INNER JOIN Person P2 WHERE = and > p2. Id;
The above SQL uses the MySQL delete syntax of the multi-table syntax I
, see: https://dev.mysql.com/doc/refman/5.0/en/delete.html
delete [ [ quick [ tbl_name [ ] ] ... from Table_references [ where where_condition ]
Solution Two:
# Write Your MySQL query statement below DELETE from INNER JOIN Person P2 WHERE = and > p2. Id;
The SQL above uses the multiple table syntax of the MySQL delete syntaxII
DELETE [low_priority] [QUICK] [IGNORE] from Tbl_name[. *][, tbl_name[.*] ] ... USING table_references [WHERE where_condition]
Solution three:???
DELETE from WHERE not inch (Select* from,selectMINfromGROUP by (Email) t);
Note that using the following SQL throws a run-time error:
# Write Your MySQL query statement below DELETE from WHERE not inch (SELECTMINfromGROUP by Email);
Runtime Error Message: You can"person" for the update in from Clauselast executed input: {" headers": {"person": ["Id", "Mail"]}, "Rows": {"person": []}}
The reasons for the above SQL error are:
In MySQL, it is forbidden to specify the target table to be updated in the FROM clause.
*delete Duplicate Emails