I have a MySQL database that has a table with multiple duplicate records. The table has 4 columns e.g. id
, post_id
, post_key
and post_value
. I am able to return the duplicate entries with the query below.
SELECT *
FROM table1 ppm
WHERE ppm.post_key = 'some_value'
AND ppm.post_id
IN
(SELECT pm.post_id FROM table1 pm
WHERE pm.post_key = 'some_value'
GROUP BY pm.post_id HAVING COUNT(pm.post_id) > 1)
It produces an output that looks like:
id, post_id, post_key, post_value
1, 2, key1, value1
2, 2, key1, value2
3, 2, key1, value3
Since there could be multiple repeating post_id
e.g. 2
, I’d like to only display the lower id
values. Using the example above, it would only show the result below since 3
is the larger number than the previous values.
id, post_id, post_key, post_value
1, 2, key1, value1
2, 2, key1, value2
How can I return duplicate rows that have a lower id
?
The current query returns all duplicate rows. I’d like to only have duplicate rows that have a lower id
.
2
Answers
Use a self-join that checks that the ID is lower in the ON condition.
I added
DISTINCT
in case there are multiple duplicates, this will only return one of each pair.See it work here: