参考自:https://blog.csdn.net/qq_35531549/article/details/90383022
Group_concat函数用于在group by分组之后,把分组隐藏的信息显示出来,默认用","分割。
首先有一张表:
1.现在需求就是根据age分组,分组后把每组的score都显示出来,使用SQL:
SELECT d.age,GROUP_CONCAT(d.score) from demo_1 d where d.id is not null group by d.age结果:
2.默认的分隔符也可以替换,如替换成"–":
SELECT d.age,GROUP_CONCAT(d.score Separator '--') from demo_1 d where d.id is not null group by d.age结果:
3.还可以去重,在函数内加DISTINCT:
SELECT d.age,GROUP_CONCAT(DISTINCT d.score) from demo_1 d where d.id is not null group by d.age结果:
4.还可以排序,在函数内加order by:
SELECT d.age,GROUP_CONCAT(d.score order by d.score desc) from demo_1 d where d.id is not null group by d.age结果: