phpmysqlmultiple-entries

Multiple values per field in a MySQL database


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, | ...  

Solution

  • 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:

    Some Annotations: