*delete Duplicate Emails

Source: Internet
Author: User
Tags mysql delete sql error

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

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.