Felix Nathan Hinkley Felix Nathan Hinkley - 1 month ago 8
MySQL Question

How to merge a field when there is a duplicate in 2 columns

If I have a table like this:

ID | Col1 | Col2 | Col3
---|------|------|-----
1 | ABC | 123 | Dogs
2 | ABC | 123 | Cats


How can I compare the values in both Col1 and Col2 and then merge the Col3 values to get this:

ID | Col1 | Col2 | Col3
---|------|------|-----
1 | ABC | 123 | Dogs, Cats

Answer
SELECT a.ID, a.Col1, a.Col2, GROUP_CONCAT(a.COl3) as Col3
FROM table a
GROUP BY a.Col1, a.Col2
Comments