Accurate SQL statement for deleting completely duplicated data in mysql

Source: Internet
Author: User

Case:
Id name Course name score
1 Zhang San math 69
2 Li Si math 89
3 Zhang San, mathematics 69
Delete redundant student information that is identical except for automatic numbers

Bytes ------------------------------------------------------------------------------------------------------
In general, the SQL statement should be:
Delete tablename where id not in (select min (id) from tablename group by name, kecheng, fenshu );
This method is supported in sqlserver or oracle, but mysql does not currently support it. An error similar to the following is reported: You can't specify target table 'tablename' for update, this is because mysql cannot query the data of a table at the same time and then delete it at the same time.
Currently, one popular solution on the internet is:
1) create a temporary table to store the columns to be queried in the temporary table
Create table temp as select...
2) operate in the temp table and original table
Delete from tablename
3) drop temp...
However, this approach not only wastes space resources, but also lacks friendliness. Through observation, we found that this type of query should solve how to distinguish the tables in the subquery from the tables in the primary query. Therefore, we can consider using aliases, put the subquery results into an alias.
The complete SQL statement is as follows:
Delete from tablename where id not in (select bid from (select min (id) as bid from tablename group by name, kecheng, fenshu) as B );
Explanation:
Select bid from (select min (id) as bid from tablename group by name, kecheng, fenshu) as B
This subquery lists the filtering results from B, that is, the set of bids.
(Select min (id) as bid from tablename group by name, kecheng, fenshu) as B
Think of the smallest bid in the grouping result as a set of hearts as a child table B of hearts,
Note that mid (id) must have an alias. Here, bid is used as a column name of B, because this column name (marked in red) is used in the upper-level query ).

Author's "kitten's column"

Related Article

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.