0

I have a Magento 2 MySQL database where one of the attributes is a varchar that contains a CSV list of IDs. Some of these fields were added to, so the list is just a string of duplicate values.

value_id attribute_id store_id entity_id value
27808 145 0 1232 16,15
29821 145 0 1327 16,15,13,29,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49
29804 145 0 1326 16,15,13,29,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49
25779 145 0 1126 16,15,13,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49
27733 145 0 1228 16,15,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,
27755 145 0 1229 16,15,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,
27791 145 0 1231 16,15,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,16,15,

Is there a way to reduce these duplicate values down to just the unique values for each row?

  • 2
    Use `explode()` to convert the string to an array, `array_unique()` to remove duplicates, then `implode()` to convert the array back to a comma-separated string. – Barmar Jun 15 '23 at 19:57
  • 1
    To do it in MySQL, see https://stackoverflow.com/questions/17942508/sql-split-values-to-multiple-rows for how to split a comma-separated value into multiple rows. Then use `GROUP_CONCAT(DISTINCT ...)` to convert it back to CSV with duplicates removed. – Barmar Jun 15 '23 at 19:59
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jun 16 '23 at 04:53
  • Provide: precise MySQL version; complete CREATE TABLE script. – Akina Jun 16 '23 at 04:59

0 Answers0