SQL statement tips: Remove duplicate rows

Source: Internet
Author: User
To remove duplicate row data from a table, you may immediately think of using the DISINTCT keyword. However, DISINTCT can only remove rows with the same columns in the table, if you need to remove rows with multiple fields in the table (that is, some are the same and some are different), what should you do? After years of database writing experience, I have compiled the following methods for your reference:

To remove duplicate row data from a table, you may immediately think of using the DISINTCT keyword. However, DISINTCT can only remove rows with the same columns in the table, if you need to remove rows with multiple fields in the table (that is, some are the same and some are different), what should you do? After years of database writing experience, I have compiled the following methods for your reference:

To remove duplicate row data from a table, you may immediately think of usingDISINTCTKeyword,DISINTCTOnly the rows with the same columns in the table can be removed. If multiple rows with the same fields need to be removed (that is, some are the same and some are different), what should we do? After years of database writing experience, I have compiled the following methods for your reference and use.

Method 1: Suitable for returning fewer Fields

Select F1, F2, F3,MAX (F4)From tablename group by F1, F2, F3

Method 2: applicable to all fields in the returned row. You must specify different fields.

Select * from tablename t whereF4= (SelectMAX (F4)From TABLENAME where T. F1 = F1 and T. F2 = F2 and T. F3 = F3)

Method 3: This method is applicable to all fields in the returned row. You must specify different fields to [find the row to be removed]

SELECT T1. * from tablename as T1, (SELECT F1, F2, MAX (F3)AS F3 from tablename group by F1, F2 having count (*)> 1) AS T2

WHERET1.F3 AND T1.F1 = T2.F1 AND T1.F2 = T2.F2

Http://www.zuowenjun.cn/post/2014/08/02/10.html

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.