2012-03-02 97 views
6

我正在尝试在mysql数据库中做重复记录。但是,我不想删除记录,只有两列是重复的。我怎样才能找到这些记录?mysql只选择从数据库重复记录

+0

http://stackoverflow.com/questions/854128/find-duplicate-records-in-mysql – 2012-03-02 22:06:32

回答

9

你能发表更多关于表结构的信息吗?你是指什么意思,有些是重复的,但只有两列?

无论如何,你可以看看到GROUP BYCOUNTHAVING

SELECT `duped_field1`, `duped_field2`, COUNT(*) `tot` 
FROM `table` 
GROUP BY `duped_field1`, `duped_field2` 
HAVING `tot` > 1 
3

查找重复的一般原则是只使用group byhaving count(*) > 1

如果你只是想知道重复列值:

select col1, col2 
from table 
group by col1, col2 
having count(*) > 1 

但是如果您想要查看其中,两列是重复的所有领域:

select t.* 
from @tbl t 
where exists (select * 
       from @tbl d 
       where d.col1 = t.col1 and d.col2 = t.col2 
       group by d.col1 
       having COUNT(*) > 1) 
+0

或只需添加'*''.. SELECT *, COUNT(*)tot' – Fabrizio 2014-09-07 18:15:15