So I have a table like this:
Item Number Collection Items in Collection
1234 Madison Park
2345 Madison Park
3456 Madison Park
6747 Belcaro
5678 Belcaro
What I need is this:
Item Number Collection Items in Collection
1234 Madison Park 1234, 2345, 3456
2345 Madison Park 1234, 2345, 3456
3456 Madison Park 1234, 2345, 3456
6747 Belcaro 6747, 5678
5678 Belcaro 6747, 5678
Or better yet, this:
Item Number Collection Items in Collection
1234 Madison Park 2345, 3456
2345 Madison Park 1234, 3456
3456 Madison Park 1234, 2345
6747 Belcaro 5678
5678 Belcaro 6747
This isn’t my preferred method of getting the result, but due to client specifications the table needs to be set up this way. Also, it needs to be done through a MySQL Workbench Query. I’ve tried every variation of CONCAT_WS and GROUP_CONCAT I can think of but haven’t managed to come up with a solution. Group_Concat returns the results I want, but then it groups the collections, which won’t work. Ideas?
Try joining the table to itself on the grouped collection value.
GROUP_CONCAT()will return a string, so you canREPLACE()the current rows’item_numberon it, however you don’t know exactly where the comma will be, so although it is a hack you can useSEPARATOR ' 'and thenREPLACE()theitem_numberfrom the current row, replace occurrences of two spaces with one (theitem_numberwas in the middle),TRIM()it (theitem_numberwas on the end) and then replace the single spaces with a comma followed by a space:I tested this locally and got your expected results.
SQLFiddle Demo