Discover delete duplicate records in sql, include the articles, news, trends, analysis and practical advice about delete duplicate records in sql on alibabacloud.com
This article introduces how to use havinggroupby and various selectin joint queries to filter the SQL language names of Repeated Records in different ways.
This article introduces how to use having group by and various select in joint queries to filter the SQL language names of Repeated Records in different ways.
1.
Delete a duplicate data SQL statementMethod One
Suppose there is a duplicate field of name,address that requires a unique result set for both fieldsSelect Identity (int,1,1) as Autoid, * into #tmp from TableNameSelect min (autoid) as autoid into #tmp2 from #tmp Group by name,autoidSELECT * from #tmp where autoid in (s
My most common method is
The code is as follows
Copy Code
Delete ID duplicate data, suitable for ID is manual primary keyDelete Person as a from as a,(Select *,min (ID) from person GROUP by ID having count (1) > 1) as Bwhere a.id = b.ID
Look for duplicates and get rid of the smallest one.
The code is as follows
Copy Code
SQL statement for querying and deleting duplicate records1. Search for redundant duplicate records in the Table. duplicate records are determined based on a single field (peopleid ).Select * from peopleWhere peopleid in (select pe
Webjxcom prompt: for example, if you want to query records with the same name, ID card number, and address in the current table (table name: peosons), select P1. * from persons P1, persons P2 where p1.idp2. ID and p1.cardid = p2.cardid and p1.pname = p2.pname and p1.address = p2.address. quantity
For example, there is a personnel table (table name: peosons)If you want to query records whose names, ID
Method One
Copy Code code as follows:
Declare @max integer, @id integer
Declare cur_rows cursor Local for select main field, COUNT (*) from table name Group by main field having count (*) > 1
Open Cur_rows
Fetch cur_rows into @id, @max
While @ @fetch_status =0
Begin
Select @max = @max-1
SET ROWCOUNT @max
Delete from table name where main field = @id
Fetch cur_rows into @id, @max
End
Close Cur_rows
SET ROWCOUNT
DatabaseThe use of the process due to procedural problems sometimes encounter duplicate data, duplication of data caused the database part of the settings can not be set correctly ...Method Onedeclare @max integer,@id integerdeclare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) > 1open cur_rowsfetch cur_rows into @id,@maxwhile @@fetch_status=0beginselect @max = @max -1set rowcount @maxdelete from 表名 where 主字段 = @id
This tutorial provides two SQL statement codes for deleting duplicate records. One is to use wherein to query duplicate records and then delete them. The other is to use innerjoin to delete
,descitem_id,max (content)From DETAILTEMP1GROUP BY catentry_id,descitem_id@SELECT * from TableA where catentry_id to (select distinct catentry_id from TableA GROUP by CATENTRY_ID,DESCITEM_ID has COUNT (*) >1) @
--5. Delete temporary table
The code is as follows
Copy Code
drop table detailtemp1@--end
There are several other ways to share
2, DB2 delete
During the use of the database, due to the problems of the program, there are sometimes duplicate data, which causes the database to be set up incorrectly.
Method One
Declare @max integer, @id integer
Declare cur_rows cursor Local for select main field, COUNT (*) from table name Group by main field having count (*) > 1
Open Cur_rows
Fetch cur_rows into @id, @max
While @ @fetch_status =0
Begin
Select @max = @max-1
SET ROWCOUNT @max
1, look for redundant records in the table, duplicate records are based on a single field (Peopleid) to determineSELECT * from Peoplewhere Peopleid in (select Peopleid from People GROUP by Peopleid have count(Peopleid) > 1)2, delete redundant records in the table,
1. Search for redundant duplicate records in the Table. duplicate records are determined based on a single field (peopleid ).
Select * from people
Where peopleid in (select peopleid from people group by peopleid having count
(Peopleid)> 1)
2. Delete unnecessary
Question: How to delete a record with the same field and leave only one record. For example, the table test contains the ID and name fields. If there are records with the same name, only one record is left, and the remaining records are deleted. The content of the name is not fixed, and the number of identical records
Label:https://leetcode.com/problems/delete-duplicate-emails/Delete Duplicate EmailsWrite a SQL query to delete all duplicate e-mail entries in a table named Person , keeping unique emai
1, for the first repetition, easier to solve, the use of
The code is as follows
Copy Code
SELECT DISTINCT * FROM tablename
You can get a result set without duplicate records.
If the table needs to delete duplicate records (1 of
SQL Delete duplicate rows (all or distinct)/*Delete a duplicate row SELECT statement Use the all or distinct option to display all rows in the table that meet the criteria, or delete duplicate
= 2Set @ startpage = 1Set @ SQL = 'select top '+ STR (@ endpage-@ startpage + 1) + 'from DBO. Employees where RID not in (select top'+ STR (@ startPage-1) + 'rid from DBO. Employees where RID> 1 )'Exec sp_executesql @ SQL-- Range QuerySelect * From DBO. Employees where birthdate between '2017-01-01 'and getdate ()Select * From DBO. Employees where birthdate not between '2017-01-01 'and getdate ()--
as follows:
SELECT DISTINCT * into #Tmp tablename
DROP TABLE TableName
SELECT * INTO TableName from #Tmp
drop table #Tmp
This duplication occurs because the table is poorly designed and can be resolved by adding a unique index column.
2. This type of repetition usually requires the first record in the duplicate record to be retained, as follows
Suppose there is a duplicate field of n
Mysql complex SQL statements (query and delete duplicate rows), mysqlsql
1. Find duplicate rows
SELECT * FROM blog_user_relation a WHERE (a.account_instance_id,a.follow_account_instance_id) IN (SELECT account_instance_id,follow_account_instance_id FROM blog_user_relation GROUP BY account_instance_id, follow_account_ins
Business: A table contains a lot of data (ID is the auto-incrementing primary key), and some of the data contains duplicate data.
Objective: To delete the duplicate data and retain the data inserted for the first time, and keep other data unaffected.
Problem solving process:
Step 1: Find the ID (save_id) of all data to be retained)
SELECT id as save_id FROM yuj
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.