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.

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)

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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.