How can I Find Duplicated Rows in a Table?

此语句对mysql 4.0以后的版本生效.
SELECT name_first, name_last, COUNT(*) AS tally,
FROM contacts
GROUP BY name_first, name_last
HAVING tally > 1;


How can I Find Duplicated Rows in a Table? [ID 1023312.1]

 Modified 17-AUG-2011     Type HOWTO     Migrated ID 5280     Status PUBLISHED 

Applies to:

MySQL Server - Version: 4.0 and later   [Release: 4.0 and later ]
All Platforms
***Checked for relevance on 17-Aug-2011***

Goal

Tips for finding duplicated rows in a table.

Solution

Finding duplicate rows in a large table can be difficult. Duplicate rows could include a table containing two or more rows in which all of their columns have the same values. This could be in a table in which there is no key column. Or it could be that there is a key column and it's different, but all of the other columns match or perhaps just a few significant columns. How duplicate rows are identified is up to you. The problem is how to easily find duplicates based on your definition.

Solution

If there are too many rows in a table to efficiently inspect the table manually, you can find them with the COUNT() function. As an example, suppose you have a table containing the names and telephone numbers of sales contacts. Suppose further that there are duplicate entries for some contacts. To find duplicate rows in a table based on a couple of columns in particular, you can enter an SQL statement like the following:

SELECT name_first, name_last, COUNT(*) AS tally,
FROM contacts
GROUP BY name_first, name_last
HAVING tally > 1;

 

This statement will list one row for each contact for which there is at least one duplicate entry (having the same first and last name) and give a tally of the number of entries for the contact. See Document 1023311.1 to learn how to to easily delete the duplicate entries.

References

http://dev.mysql.com/doc/refman/5.5/en/select.html
NOTE:1023311.1 - How can I delete duplicate rows?
http://dev.mysql.com/doc/refman/5.5/en/group-by-functions.html#function_count

Show Related InformationRelated


Products
  • Oracle Database Products > MySQL > MySQL Server > MySQL Server
Keywords
DUPLICATE ENTRY; MYSQL

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/11780477/viewspace-705238/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/11780477/viewspace-705238/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值