Duplicate records in SQL query table repeat the last record method

Source: Internet
Author: User
Tags repetition rowcount access database

SQL statements remove duplicate records, get duplicate records

--Query a table to effectively remove duplicate records, UserID as a self-growing primary key, Roleid as a repeating field

The code is as follows Copy Code

SELECT MIN (UserID) as UserID, Roleid from Tmptable GROUP by Roleid

SELECT Roleid from Tmptable GROUP by Roleid (COUNT (*) > 1)

SELECT DISTINCT * from tmptable

SQL statement query for all table names in SQL Server Access database, field name February 01, 2007 Thursday PM 04:21sql Server

View all table names:

The code is as follows Copy Code

Select name from sysobjects where type= ' U '

Query table for all field names:

The code is as follows Copy Code


Select name from syscolumns Where id=object_id (' Table name ')

SELECT * FROM Information_schema.tables
SELECT * FROM Information_schema.views
SELECT * FROM Information_schema.columns

ACCESS

View all table names:

The code is as follows Copy Code
Select name from Msysobjects where type=1 and flags=0

The msysobjects is a system object, and the default is hidden. Make it visible through tools, options, views, displays, and system objects

Cases

As shown in the following table:

The code is as follows Copy Code

CREATE TABLE ' T1 ' (
' UserID ' INT (one) DEFAULT NULL,
' Atime ' datetime DEFAULT NULL,
KEY ' Idx_userid ' (' userid ')
) Engine=innodb DEFAULT Charset=utf8;

The data are as follows:

The code is as follows Copy Code

Mysql> SELECT * from T1;
+--------+---------------------+
| UserID | Atime |
+--------+---------------------+
| 1 | 2013-08-12 11:05:25 |
| 2 | 2013-08-12 11:05:29 |
| 3 | 2013-08-12 11:05:32 |
| 5 | 2013-08-12 11:05:34 |
| 1 | 2013-08-12 11:05:40 |
| 2 | 2013-08-12 11:05:43 |
| 3 | 2013-08-12 11:05:48 |
| 5 | 2013-08-12 11:06:03 |
+--------+---------------------+
8 ROWS in SET (0.00 sec)

The UserID is not unique, requiring that each userid in the table be taken from a recent record in the present time. At first sight this condition is generally thought to borrow temporary tables and add the main build with the help of the join operation.
Give a simple method:

The code is as follows Copy Code

Mysql> SELECT Userid,substring_index (Group_concat (atime order by Atime DESC), ",", 1) as atime to T1 group by UserID;
+--------+---------------------+
| UserID | Atime |
+--------+---------------------+
| 1 | 2013-08-12 11:05:40 |
| 2 | 2013-08-12 11:05:43 |
| 3 | 2013-08-12 11:05:48 |
| 5 | 2013-08-12 11:06:03 |
+--------+---------------------+
4 ROWS in SET (0.03 sec)


Say
There is a field "name" in Table A,
and the "name" value may be the same between different records,
Now you need to query the records in the table, the "name" value duplicates the item;

The code is as follows Copy Code
Select Name,count (*) from-A Group by Name has Count (*) > 1

If the same gender is also the same large as the following:

The code is as follows Copy Code
Select Name,sex,count (*) from-A Group by Name,sex have Count (*) > 1


Three
Method One

The code is as follows Copy Code

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 0

Method Two

There are two duplicates of the record, one is a completely duplicate record, that is, all the fields are duplicate records, the second is some key fields duplicate records, such as the Name field repeat, and other fields do not necessarily repeat or repeat can be ignored.

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 duplicate records are retained), you can delete them in the following ways

The code is as follows Copy Code

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 name,address that requires a unique result set for both fields

The code is as follows Copy Code

Select Identity (int,1,1) as Autoid, * into #Tmp from TableName

Select min (autoid) as autoid into #Tmp2 from #Tmp Group by name,autoid

SELECT * from #Tmp where autoid in (select Autoid from #tmp2)

The last select gets the name,address result set (but one more autoid field, which can be written in the SELECT clause to omit this column)

Four

Query duplication

  code is as follows copy code

Select * From TableName where ID in (

Select ID from tablename

Group by ID

have count (id) > 1

)

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.