Finding mySQL duplicates, then merging data

Posted by Michael Pasqualone on Stack Overflow See other posts from Stack Overflow or by Michael Pasqualone
Published on 2010-05-24T11:21:25Z Indexed on 2010/05/24 11:31 UTC
Read the original article Hit count: 207

Filed under:
|

I have a mySQL database with a tad under 2 million rows. The database is non-interactive, so efficiency isn't key.

The (simplified) structure I have is:

`id` int(11) NOT NULL auto_increment
  `category` varchar(64) NOT NULL
  `productListing` varchar(256) NOT NULL

Now the problem I would like to solve is, I want to find duplicates on productListing field, merge the data on the category field into a single result - deleting the duplicates.

So given the following data:

+----+-----------+---------------------------+
| id | category  | productListing            |
+----+-----------+---------------------------+
|  1 | Category1 | productGroup1             | 
|  2 | Category2 | productGroup1             | 
|  3 | Category3 | anotherGroup9             | 
+----+-----------+---------------------------+

What I want to end up is with:

+----+----------------------+---------------------------+
| id | category             | productListing            |
+----+----------------------+---------------------------+
|  1 | Category1,Category2  | productGroup1             | 
|  3 | Category3            | anotherGroup9             | 
+----+----------------------+---------------------------+

What's the most efficient way to do this either in pure mySQL query or php?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about php5