Storing data in comma separated strings wasn't up to me and it isn't something I can change in my database so bear with me. I did quite a bit of searching already online and on stackoverflow but I couldn't find a solution to this, if it's even possible using MySQL.
I am trying to replace all instances of every unique string from table1 with a matching value from table2. I have tried wildcards, replace, update, join, etc and I'm just not sure how to make it work. I know one solution would be replace() for each string but table2 has over 200 rows so that would mean nesting over 200 times.
Here's what I'm trying to accomplish. I have two tables, table1:
+------+-------------+
| Item | Code |
+------+-------------+
| 1 | 614 |
+------+-------------+
| 2 | 212,614,415 |
+------+-------------+
| 3 | 212,303 |
+------+-------------+
| ... | ... |
+------+-------------+
and table2:
+------+-------------------+
| Code | Name |
+------+-------------------+
| 614 | Columbus, OH |
+------+-------------------+
| 212 | New York, NY |
+------+-------------------+
| 415 | San Francisco, CA |
+------+-------------------+
| 303 | Ft. Worth, TX |
+------+-------------------+
| ... | ... |
+------+-------------------+
I want to replace codes from table1 with the corresponding values from table2 to produce this result:
+------+---------------------------------------------+
| Item | Code |
+------+---------------------------------------------+
| 1 | Columbus, OH |
+------+---------------------------------------------+
| 2 | New York, NY,Columbus, OH,San Francisco, CA |
+------+---------------------------------------------+
| 3 | New York, NY,Ft. Worth, TX |
+------+---------------------------------------------+
| ... | ... |
+------+---------------------------------------------+