Delete Post Meta with Duplicate Values

I need to remove duplicate meta_values from a table where the duplicate values share the same post_id. An import went wrong and there’s several hundred thousand rows with a lot of duplicate values.

So in the example below there are two rows with post_id 12 and meta_value of 6 when there should only be one.

meta_id    post_id    meta_key               meta_value
-------    -------    --------               ----------
1          12         attribute_pa_length    6
2          12         attribute_pa_length    6
3          12         attribute_pa_length    9
4          35         attribute_pa_length    8
5          35         attribute_pa_length    12

Related posts

1 comment

  1. This will leave you with the smaller meta_id

    DELETE T1
    FROM YourTable T1, YourTable T2
    WHERE T1.post_id = T2.post_id   
      AND T1.meta_value = T2.meta_value
      AND T1.meta_id > T2.meta_id    
    

Comments are closed.