在MySQL 2中查找重复记录

时间:2012-02-02 16:47:15

标签: mysql duplicate-data duplicates

所有人都可能会建议我在此网址http://senta.la/bd08

中阅读以前的问题

所以我只是仔细阅读它们,我仍然遇到问题。这种情况不等于上面链接中提到的问题。

请参阅下面我想找到的记录(标有*):

ID         COURSE       OTHERCOLUMN_X      OTHERCOLUMN_Y       OTHERCOLUMN_Z
123        2111         ...                ...                 ...
*5644      2245         ...                ...                 ...
*5644      2245         ...                ...                 ...
2121       2131         ...                ...                 ...

以前的程序员没有考虑使用UNIQUE索引来制作堆肥密钥以防止出现这种情况。我知道......好消息是程序员不再是程序员了。

这个经典查询不提供我想要的结果......

SELECT id, course FROM mytable
GROUP BY id 
HAVING COUNT(id) > 1

我想只看到这个结果:

ID         COURSE       OTHERCOLUMN_X      OTHERCOLUMN_Y       OTHERCOLUMN_Z
*5644      2245         ...                ...                 ...
*5644      2245         ...                ...                 ...

我只是尝试了很多查询,但我可以找到一个向我展示上述结果的查询。

:(

4 个答案:

答案 0 :(得分:1)

好的

SELECT id, course FROM mytable GROUP BY id  HAVING COUNT(id) > 1 

一种方法是加入回原始表

这样的东西
Select myTable.ID,myTable.Course,... From myTable
inner join(SELECT id, course FROM mytable GROUP BY id  HAVING COUNT(id) > 1) duplicates
On duplicates.id = mytable.id and duplicates.course = mytable.course

也许

答案 1 :(得分:1)

尝试使用以下内容:

SELECT id, 
COUNT(id) AS NumOccurrences
FROM YourTableName
GROUP BY id
HAVING ( COUNT(id) > 1 )

答案 2 :(得分:1)

要查找包含重复id的行:

SELECT a.*
FROM mytable AS a
  JOIN
    ( SELECT id 
      FROM mytable
      GROUP BY id 
      HAVING COUNT(*) > 1
    ) AS b
    ON b.id = a.id

要查找包含重复(id, course)的行:

SELECT a.*
FROM mytable AS a
  JOIN
    ( SELECT id, course 
      FROM mytable
      GROUP BY id, course 
      HAVING COUNT(*) > 1
    ) AS b
    ON  b.id = a.id
    AND b.course = a.course

答案 3 :(得分:0)

可能是这样的:

SELECT id, course 
FROM mytable
WHERE id in (SELECT id from mytable GROUP BY id HAVING COUNT(id) > 1)