GROUP_CONCAT 多行合并

MySQL version:8.0.18-commercial MySQL Enterprise Server - Commercial

语法:
GROUP_CONCAT([DISTINCT] expr [,expr ...]
             [ORDER BY {unsigned_integer | col_name | expr}
                 [ASC | DESC] [,col_name ...]]
             [SEPARATOR str_val])
示例:
mysql> SELECT student_name,
         GROUP_CONCAT(test_score)
       FROM student
       GROUP BY student_name;
Or:

mysql> SELECT student_name,
         GROUP_CONCAT(DISTINCT test_score
                      ORDER BY test_score DESC SEPARATOR ' ')
       FROM student
       GROUP BY student_name;   

注意:
多行合并的结果会被截断,结果的长度由 group_concat_max_len 控制,默认是1024,最大值可以设置,但是不能超过max_allowed_packet。

SET [GLOBAL | SESSION] group_concat_max_len = val;

官方这句话,是什么意思。。看着不是很懂,应该和结果(result)有关

  The return value is a nonbinary or binary string, depending on whether the arguments are nonbinary or binary strings. 
  The result type is TEXT or BLOB unless group_concat_max_len is less than or equal to 512, in which case the result type is VARCHAR or VARBINARY.
posted @ 2020-06-27 16:13  Coye  阅读(253)  评论(0编辑  收藏  举报