2014-06-20 71 views
0

我試圖找到每個客戶的重複記錄。在每個客戶ID中查找重複的記錄在MySQL中

表: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

只需運行y我們的內在查詢本身。 – Vatev

+0

請查看http://sqlfiddle.com/#!2/071ad/3 – mrdeveloper

回答

3

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