Jump to content

how to get all the duplicates based on a field


jasonc

Recommended Posts

I am wanting to get all results where there is a duplicate 'Pid'

 

Pid is the Product ID.

 

 

INSERT INTO `products` (`Pid`, `productTitle`) VALUES

(3350, 'Product A'),

(3351, 'Product B'),

(3352, 'Product C'),

(3353, 'Product D'),

(3354, 'Product E'),

(3354, 'Product F'),

(3355, 'Product G'),

(3355, 'Product H'),

(3356, 'Product I'),

(3356, 'Product J');

 

from this table the results should be.

 

 

(3354, 'Product E')

(3354, 'Product F')

(3355, 'Product G')

(3355, 'Product H')

(3356, 'Product I')

(3356, 'Product J')

 

 

what formular should i use for this to work.

Link to comment
Share on other sites

To get a list of the duplicate Pid values, you would use:

SELECT Pid FROM products GROUP BY Pid HAVING COUNT(Pid) > 1

 

Since you want the results to include other details from the row, you have to use that query in a WHERE clause:

 

SELECT Pid, productTitle
FROM products
WHERE Pid IN (SELECT Pid FROM products GROUP BY Pid HAVING COUNT(Pid) > 1)

Link to comment
Share on other sites

Hey thanks very well explained.  I see where I was going wrong now.

 

Thanks for your help on this.

 

 

To get a list of the duplicate Pid values, you would use:

SELECT Pid FROM products GROUP BY Pid HAVING COUNT(Pid) > 1

 

Since you want the results to include other details from the row, you have to use that query in a WHERE clause:

 

SELECT Pid, productTitle
FROM products
WHERE Pid IN (SELECT Pid FROM products GROUP BY Pid HAVING COUNT(Pid) > 1)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.