使用MySQL,我想SELECTmultiple列并使用GROUP BY,但只显示按分组列的分组

时间:2016-04-27 14:40:37

标签: mysql

目前,每行都会显示按行分组的列。我想只在分组更改时显示列。

例如:

water  sea
       ocean
       river
sand   beach
       desert
trees  woods
       forest

1 个答案:

答案 0 :(得分:0)

如果您必须在MySQL中执行此操作,则可以执行以下操作:

select your_column1, group_concat(distinct your_column2 separator '\n') from your_table 
group by your_column1;

your_column1 | your_column2
----------------------------
water        | sea
----------------------------
water        | ocean
----------------------------
water        | river
----------------------------
sand         | beach
----------------------------
sand         | desert
----------------------------
trees        | woods
----------------------------
trees        | forest

your_column1 | your_column2
----------------------------
water        | sea
             | ocean
             | river
----------------------------
sand         | beach
             | desert
----------------------------
trees        | woods
             | forest