,282,3358,123,
Table B
| ID | name |
+----+-------------------+
|282 | name_1 |
+----+-------------------+
|3358| name_2 |
+----+-------------------+
|123 | name_3 |
Initial Situation TABLE A
... | ID | ...
-----+------------------------+-----
... | ,282,3358,123, | ...
Desired Result for TABLE A
... | ID | ...
-----+------------------------+-----
... | ,name_1,name_2,name_3, | ...
I found a way how do deal with my problem. However it's really not recommendable to use multiple entries per field. But if your in a situation like me, where you have to work with such an unrelational schemed database without being allowed to change the scheme, here we are:
SELECT [...] ,
(SELECT GROUP_CONCAT( table_B.name SEPARATOR ',' ) FROM table_B
WHERE FIND_IN_SET ( B.ID , SUBSTRING ( table_A.ID , 2 , length( table_A.ID ) -2 ) ) >0 ) ,
[...]
FROM table_A
Some Explanations:
SUBSTRING
is needed in order to remove the "," in the begining of the multiple field entry. E.g.: SUBSTRING ( ",282,3358,123," , 2 , length( ",282,3358,123," ) -2 )
results in "282,3358,123"FIND_IN_SET
method searches a value into a comma separated string list and returns the index of the value. If no value can be found, it returns 0. Thats why I compare the whole string against 0.GROUP_CONCAT
method concatenates the matching names. As a result it delivers a comma separated string list ==> name_1,name_2,name_3
Some Annotations: