Search code examples
phpmysqlmergecreate-table

Merge three mysql entries to one entry


I have 3 or 4 entries with the same name of a person an different other entries.

A    B    C          
---- ---- -----------
a    01    XXX 
a    02    XYZ
a    03    ABC

How can I merge them by selecting the a which is always the same in A to a structure like this:

A    B    C    D    E    F    G
---  ---  ---  ---  ---  ---  --- 
a    01   02   03   XXX  XYZ  ABC

Hope you can help me getting this.


Solution

  • Alternative Solution

    If you have variable number of entries for different users then use GROUP_CONCAT FUNCTION. It is easier way (with some processing in php) than to make dynamic number of columns in sql.

    SELECT A,GROUP_CONCAT(B) B, GROUP_CONCAT(C) C
    FROM TABLE 
    GROUP BY A;
    

    The result will be like:- A B C
    --- --- --- a 01,02,03 XXX,XYZ,ABC

    You can further process it in php code to get column separated.