41

This question is pretty simple I for some reason can't get the proper result to display only the duplicate records

Table   : Paypal_ipn_orders
id                              payer_email
1                               [email protected]
2                               [email protected]   
3                               [email protected]
4                               [email protected]
5                               [email protected]

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

sample output

id       tot
1         2
4         2

expected output

id       payer_email 
1        [email protected]
3        [email protected]
4        [email protected]
5        [email protected]

How do I make this happen?

12 Answers 12

78
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

Sign up to request clarification or add additional context in comments.

7 Comments

that returns this unknown table status: TABLE_TYPE unknown table status: TABLE_TYPE and Showing rows 0 - -1 ( 0 total, Query took 39.6457 sec)
@user1542036 That has nothing to do with this query. It is correct (check the sqlfidddle). Do a Google search on your error.
the only thing I find is it appears to be a problem with the particular version of phpmyadmin? I'm running 3.4.1 so upgrading to the newest version should fix it?
very slow in large table.
Yes it is slow in a large table or complex query we should use join (inner join) instead of this
|
19

The IN was too slow in my situation (180 secs)

So I used a JOIN instead (0.3 secs)

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

1 Comment

Use SELECT i.id, i.payer_email FROM paypal_ipn_orders i otherwise it will show ambiguous error
8

here is the simple example :

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

It will definitly work. :)

1 Comment

this is brilliant!
2

Get a list of all duplicate rows from table:

Select * from TABLE1 where PRIMARY_KEY_COLUMN NOT IN ( SELECT PRIMARY_KEY_COLUMN
FROM TABLE1 
GROUP BY DUP_COLUMN_NAME having (count(*) >= 1))

3 Comments

By far the fastest, but you need to remove the "=" from count(*) >= 1 because it will take the ids that exists only once too.
Stack Overflow needs to allow <6 character edits. >= is simply wrong here.
@Incognito - NO, the purpose of >= is to INCLUDE non-duplicate rows; then NOT IN returns the "extra" rows when there are duplicates. Note that this is slighly different than OP's request, which would list BOTH rows of each duplicate. If you do >, then NOT IN will give you many non-duplicate rows. In that case, if used the resulting IDs to delete duplicates, the result would be disastrous!
1

Hi above answer will not work if I want to select one or more column value which is not same or may be same for both row data

For Ex. I want to select username, birth date also. But in database is username is not duplicate but birth date will be duplicate then this solution will not work.

For this use this solution Need to take self join on same table/

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

Above query will return all records having same email_id and same birth date

1 Comment

Minor nit: Likely faster to replace WHERE with AND so the filtering is done during the INNER JOIN, rather than afterwards.
1

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
    );

2 Comments

This will not give the correct result as you are excluding the duplicate record change NOT IN to IN
@Abhinavbhardwaj - I've tested this answer, and it works. The inner SELECT finds keys of all NOT-DUPLICATED column values. Then NOT IN excludes those, thereby finding all DUPLICATES.
1

use this code

 SELECT *  
    FROM  paypal_ipn_orders 
    GROUP BY  payer_email  
    HAVING COUNT( payer_email) >1  

1 Comment

Not correct. will show only one row per GROUP BY value. What happens if there are THREE with same email? This will only list ONE of them. (If that is rare, could run this again, to catch any remaining duplicates. Repeat until zero rows returned.)
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)

1 Comment

this also returns the samething as the others
0

Similar to this answer, though I used a temporary table instead:

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;

Comments

0
SELECT * FROM `table` t1 join `table` t2 WHERE (t1.name=t2.name) && (t1.id!=t2.id)

1 Comment

Can you explain your solution to the problem? You may also use the code indentation feature of the editor to highlight the SQL (four spaces).
-1

I think this way is the simplier. The output displays the id and the payer's email where the payer's email is in more than one record at this table. The results are sorted by id.

    SELECT id, payer_email
    FROM paypal_ipn_orders
    WHERE COUNT( payer_email )>1
    SORT BY id;

1 Comment

MySQL response: Not a valid query: "Invalid use of a group function". It makes no sense to use COUNT like this, without GROUP BY.
-2

Try this query:

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

Does it help?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.