MySQL Group by one column, but select all data

后端 未结 3 1401
无人共我
无人共我 2021-01-12 04:42

I\'ve got a database which contains lots of data. I would like to select all data, but Group by one column.

So for example:

column a | column b
examp         


        
3条回答
  •  日久生厌
    2021-01-12 05:46

    I think you can get what you want using group_concat():

    select a, group_concat(b)
    from t
    group by a;
    

    This will create a list of "b"s for each a. In your example:

    example    apple,pear,orange,strawberry
    

    You can change the separator using the SEPARATOR keyword.

    EDIT:

    You can use group_concat() multiple times:

    select a, group_concat(b) as bs, group_concat(c) as cs
    from t
    group by a;
    

    Or, combine it with concat():

    select a, group_concat(concat(b, ':', 'c')) as bcs
    from t
    group by a;
    

提交回复
热议问题