For repeated row deletion problems, it is difficult to find a suitable answer on the Internet and there are a lot of questions, but there is no solution to the previous record in the search engine.
In fact, this problem can be effectively solved.
1. If the table does not have a primary key (or the same row does not have different content columns), you need to create an auto-increment column to distinguish different columns. For example
Copy codeThe Code is as follows: alter table [tablename] add [TID] int IDENTITY (1, 1)
Is to add an incremental temporary column TID.
Why SQL statements? If the number of rows exceeds 100,000, modification to the design interface of the SQL SERVER Enterprise Manager usually times out.
2. Then it is the key. Here is an example of the IP address location list that I want to handle. There are nearly 0.4 million pieces of data, with repeated records of SIP and EIP (start IP and end IP). The number of duplicates accounts for about 1/5. To solve this problem, use a simple SQL command:Copy codeThe Code is as follows: delete from query_IP where TID not in (select max (TID) from query_IP group by SIP, EIP)
That is, the maximum TID value of the same group is obtained by grouping the SIP and EIP. Then, delete the content that is not in the original table (that is, smaller ID content in the duplicate content of the same group.
This idea can be used to extend a lot of ways to solve SQL problems. For example, a user login table needs to view the recent login records of each user.
An elegant query statement:Copy codeThe Code is as follows: Select * from LoginLog where ID in (select max (ID) from LoginLog group by UserID)
SQL is extremely powerful. Many complex requirements can be combined into a single SQL statement query. Therefore, in my program, except for the UPDATE/INSERT operations, transaction support is required, or the number of records is too large, which requires paging or temporary tables. It is usually implemented using an SQL statement. For example, select *, (select count (*) from xxx where xxx = t. ID) from t where .... In this way, the associated statistical items can be obtained in the SELECT statement, which is especially useful for small and medium systems.