36

这个问题很简单,我由于某种原因无法得到正确的结果来只显示重复的记录

Table   : Paypal_ipn_orders
id                              payer_email
1                               susan@gmail.com
2                               ryan@gmail.com   
3                               susan@gmail.com
4                               steve@gmail.com
5                               steve@gmail.com

SELECT id, COUNT( payer_email ) `tot`
FROM paypal_ipn_orders
GROUP BY payer_email
HAVING `tot` >1

样本输出

id       tot
1         2
4         2

预期产出

id       payer_email 
1        susan@gmail.com
3        susan@gmail.com
4        steve@gmail.com
5        steve@gmail.com

我该如何做到这一点?

4

12 回答 12

74
SELECT id, payer_email
FROM paypal_ipn_orders
WHERE payer_email IN (
    SELECT payer_email
    FROM paypal_ipn_orders
    GROUP BY payer_email
    HAVING COUNT(id) > 1
)

sqlfiddle

于 2012-07-27T19:56:04.330 回答
15

在我的IN情况下太慢了(180秒)

所以我改用了JOIN(0.3秒)

SELECT i.id, i.payer_email
FROM paypal_ipn_orders i
INNER JOIN (
 SELECT payer_email
    FROM paypal_ipn_orders 
    GROUP BY payer_email
    HAVING COUNT( id ) > 1
) j ON i.payer_email=j.payer_email
于 2014-01-28T14:23:04.247 回答
7

这是一个简单的例子:

select <duplicate_column_name> from <table_name> group by <duplicate_column_name> having count(*)>=2

它肯定会起作用。:)

于 2015-12-17T10:38:33.210 回答
2

从表中获取所有重复行的列表:

Select * from TABLE1 where PRIMARY_KEY_COLUMN NOT IN ( SELECT PRIMARY_KEY_COLUMN
FROM TABLE1 
GROUP BY DUP_COLUMN_NAME having (count(*) >= 1))
于 2014-03-03T09:36:03.960 回答
1

嗨,如果我想为两个行数据选择一个或多个不同或可能相同的列值,上述答案将不起作用

对于前。我也想选择用户名,出生日期。但是在数据库中,用户名不重复但出生日期将重复,则此解决方案将不起作用。

为此使用此解决方案需要在同一张表上进行自我加入/

SELECT  
    distinct(p1.id),  p1.payer_email , p1.username, p1.birth_date

FROM 
    paypal_ipn_orders AS p1 

INNER JOIN paypal_ipn_orders AS p2 

ON p1.payer_email=p2.payer_email

WHERE 

p1.birth_date=p2.birth_date

上面的查询将返回所有具有相同 email_id 和相同出生日期的记录

于 2014-03-28T10:05:09.110 回答
1

使用此代码

 SELECT *  
    FROM  paypal_ipn_orders 
    GROUP BY  payer_email  
    HAVING COUNT( payer_email) >1  
于 2016-04-27T11:46:37.700 回答
0

This works the fastest for me

SELECT
    primary_key
FROM
    table_name
WHERE
    primary_key NOT IN (
        SELECT
            primary_key
        FROM
            table_name
        GROUP BY
            column_name
        HAVING
            COUNT(*) = 1
    );
于 2014-09-11T17:22:58.207 回答
0
SELECT id, payer_email FROM paypal_ipn_orders
WHERE payer_email IN (
    SELECT payer_email FROM papypal_ipn_orders GROUP BY payer_email HAVING COUNT(*) > 1)
于 2012-07-27T19:58:09.150 回答
0
SELECT * FROM `table` t1 join `table` t2 WHERE (t1.name=t2.name) && (t1.id!=t2.id)
于 2016-04-27T14:49:13.320 回答
0

此答案类似,尽管我使用了临时表:

CREATE TEMPORARY TABLE duplicates (
    SELECT payer_email
    FROM paypal_ipn_orders
    GROUP BY payer_email
    HAVING COUNT(id) > 1
);
SELECT id, payer_email
FROM paypal_ipn_orders AS p
INNER JOIN duplicates AS d ON d.payer_email=p.payer_email;
于 2016-01-11T17:42:35.210 回答
-1

我认为这种方式更简单。输出显示 id 和付款人的电子邮件,其中付款人的电子邮件在此表的多个记录中。结果按 id 排序。

    SELECT id, payer_email
    FROM paypal_ipn_orders
    WHERE COUNT( payer_email )>1
    SORT BY id;
于 2015-06-07T21:04:13.703 回答
-2

试试这个查询:

SELECT id, COUNT( payer_email ) `tot`
FROM paypal_ipn_orders
GROUP BY id
HAVING `tot` >1

它有帮助吗?

于 2012-07-27T19:53:02.877 回答