简体   繁体   中英

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.

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:- ABC
--- --- --- a 01,02,03 XXX,XYZ,ABC

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM