MYSQL中的COUNT DUPLICATE RECORDS

时间:2014-03-06 10:08:09

标签: mysql

我需要一个查询来计算重复记录,

例如

table name -customer
===================

customer_id-col name
222
111
222
222
111
122

输出

customer_id   count

222              3
111              2
222              3
222              3
111              2
122              1 

我试过这个查询

SELECT customer_id,count(customer_id)c 来自客户 GROUP BY customer_id 有c> 1

输出

customer_id count

222              3
111              2
122              1 

这是否可能提前谢谢

由于 拉加

2 个答案:

答案 0 :(得分:1)

试试这个

SELECT T.customer_id,S.duplicate_count FROM
    (
    SELECT customer_id,count(customer_id) AS duplicate_count
    FROM yourtable group by customer_id
    HAVING (duplicate_count > 0)
    ) AS S Join yourtable On S.customer_id = T.customer_id

<强> FIDDLE DEMO

<强> OP:

 customer_id       count

    222              3
    111              2
    222              3
    222              3
    111              2
    122              1 

答案 1 :(得分:0)

select customer_id,count(1) as count
from customer
group by customer_id