在MySQL中查找每个客户ID的重复记录

时间:2014-06-20 15:13:58

标签: mysql sql having

我尝试为每个客户找到重复的记录。

表:customers_points

points_id + customers_id + points_comment 
    1           200          From Order: 5227 
    2           200          From Order: 5227 
    3           200          From Order: 5227 
    4           201          From Order: 7000 
    5           201          From Order: 7000 
    6           202          From Order: 6000 
    7           203          From Order: 8000 

结果应该是这样的:

customers_id  + duplicate_rows
    200              3
    201              2

下面是我当前的sql查询(不准确):

SELECT c.* 
FROM  `customers_points` c
INNER join (SELECT points_comment FROM `customers_points` GROUP BY points_comment HAVING count(customers_id) > 1) dup 
ON c.points_comment = dup.points_comment
WHERE c.points_date_added > '2014-04-01 12:01:00'
GROUP BY c.customers_id
ORDER BY c.points_date_added DESC limit 100

1 个答案:

答案 0 :(得分:3)

SELECT customers_id, count(points_comment) duplicate_rows FROM customers_points GROUP BY customers_id, points_comment