返回所有重复的行

时间:2012-03-05 22:12:45

标签: sql sqlite select group-by duplicates

我已经编写了这段代码来查找重复内容并且工作正常:

SELECT *
FROM StyleTable 
GROUP BY Color
HAVING count(*) > 1 

问题是,它只返回一个重复的行。是否可以返回所有重复的行?我猜这可能与'GROUP BY'有关,但我不知道如何改变它。我不想删除它们,只需返回它们。

3 个答案:

答案 0 :(得分:18)

你必须再次加入桌面以获得重复的内容。类似的东西:

SELECT * 
FROM StyleTable 
WHERE Color IN (
  SELECT Color  
  FROM StyleTable   
  GROUP BY Color  
  HAVING count(*) > 1 
)     

答案 1 :(得分:6)

SELECT s.*
    FROM StyleTable s
        INNER JOIN (SELECT Color
                        FROM StyleTable
                        GROUP BY Color
                        HAVING COUNT(*) > 1) q
            ON s.Color = q.Color

答案 2 :(得分:0)

WITH cte AS ( SELECT FirstName, LastName,COUNT(*) occurrences FROM Customer GROUP BY FirstName, LastName HAVING COUNT(*) > 1)
SELECT * FROM Customer INNER JOIN cte ON 
        cte.FirstName = Customer.FirstName AND 
        cte.LastName = Customer.LastName
ORDER BY Customer.FirstName, Customer.LastName