This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values. The full syntax is as follows:

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;

  

In MySQL, you can get the concatenated values of expression combinations. To eliminate duplicate values, use the DISTINCT clause. To sort values in the result, use the ORDER BY clause. To sort in reverse order, add theDESC (descending) keyword to the name of the column you are sorting by in the ORDER BY clause. The default is ascending order; this may be specified explicitly using the ASC keyword. The default separator between values in a group is comma (“,”). To specify a separator explicitly, use SEPARATOR followed by the string literal value that should be inserted between group values. To eliminate the separator altogether, specify SEPARATOR ''.

The result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. The value can be set higher, although the effective maximum length of the return value is constrained by the value of max_allowed_packet. The syntax to change the value ofgroup_concat_max_len at runtime is as follows, where val is an unsigned integer:

SET [GLOBAL | SESSION] group_concat_max_len = val;

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. (Prior to MySQL 5.0.19, GROUP_CONCAT() returned TEXT or BLOBgroup_concat_max_len greater than 512 only if the query included an ORDER BY clause.)

quoted from:

https://dev.mysql.com/doc/refman/5.5/en/group-by-functions.html#function_group-concat

GROUP_CONCAT(expr)的更多相关文章

  1. mysql行转列,函数GROUP_CONCAT(expr)

    demo: 语句: SELECT '行' id, '' product_nameUNIONSELECT id, product_name FROM `product` WHERE id < 5 ...

  2. 【MySQL】MySQL的group_concat使用例子

    > 参考的优秀文章 GROUP_CONCAT(expr) > 简单的例子 此函数的功能,是拼接group分组多行的数据为一行,并可以指定去重.排序.分隔符. 例子: select t.na ...

  3. MySQL拼接字符串,GROUP_CONCAT 值得拥有

    上一篇文章 跨表更新,看到自己写的SQL像个憨憨写了关于跨表个更新的内容.一年过的很快,文中后来的两位员工 馮大 和 馮二 也要面对无情的 KPI 考核了,他们工作干的很不错,performance ...

  4. 分组拼接字符串,GROUP_CONCAT

    背景 一年过的很快,文中后来的两位员工 馮大 和 馮二 也要面对无情的 KPI 考核了,他们工作干的很不错,performance 分别是 4 和 5 新需求来了,静悄悄的来了!!! 领导想要查看每个 ...

  5. MySQL、DM 行转列及字段去重(Group_Concat())

    最近在使用数据库迁移适配,由MySQL 库迁移到达梦数据库,其中进行行转列时,MySQL转换达梦sql语法有些问题,特记录. 在MySQL 下有Group_Concat(expr)  ,在达梦及神通数 ...

  6. 我的MYSQL学习心得(六) 函数

    我的MYSQL学习心得(六) 函数 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类 ...

  7. mysql函数大全

    对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的最左面字符的ASCII代码值.如果str是空字符串,返回0.如果str是NULL,返回NULL. mysql& ...

  8. MySQL 函数大全

    mysql函数大全 对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的最左面字符的ASCII代码值.如果str是空字符串,返回0.如果str是NULL,返回NU ...

  9. mysql常用函数参考

    mysql常用函数参考   对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的最左面字符的ASCII代码值.如果str是空字符串,返回0.如果str是NULL, ...

随机推荐

  1. ZooKeeper异常:Error connecting service It is probably not running

    ZooKeeper安装后使用以下命令可以启动成功 bin/zkServer.sh start 但是使用下面命令查看启动状态,则报错误: bin/zkServer.sh status Error con ...

  2. python学习之常用模块

  3. DHT11资料

    产品名:温湿度传感器 型号:DHT11 厂商:奥松电子 参数: 相对湿度: 分辨率:0.1%RH        16Bit 精度:25℃  正负 %2 温度: 分辨率:0.1%RH        16 ...

  4. Qt5 调试之详细日志文件输出(qInstallMessageHandler)

    注明:以下方法仅适用于 Qt5 及以上版本  函数说明: QtMessageHandler qInstallMessageHandler(QtMessageHandler handler) 此函数在使 ...

  5. R语言绘图:直方图

    使用ggplot2包绘制直方图 ######*****绘制直方图代码*****####### data1 <- data0[(data0[, 2] <= 500) & (data0 ...

  6. Tensorflow之MNIST的最佳实践思路总结

    Tensorflow之MNIST的最佳实践思路总结   在上两篇文章中已经总结出了深层神经网络常用方法和Tensorflow的最佳实践所需要的知识点,如果对这些基础不熟悉,可以返回去看一下.在< ...

  7. Android面试收集录 网络与加密

    1.创建Socket对象需要至少指定哪些信息? IP(或域名)和端口号 Socket socket=new Socket("www.baidu.com",80); 2.如何使用So ...

  8. c/c++ 数组传参

    在c/c++中,在进行数组传参时,数组的元素个数默认是不作为实参传入调用函数,也就是说c/c++ 不允许向函数传递一个完整的数组作为参数 实例: 1.形式参数是一个指针,实参包括数组长度: 1 voi ...

  9. How to find your web part

         When we deploy a web part, we can find it on any pages through the follow steps:      Firstly, ...

  10. DDoS 攻击与防御:从原理到实践(下)

    欢迎访问网易云社区,了解更多网易技术产品运营经验. DDoS 攻击与防护实践 DDoS 攻击的实现方式主要有如下两种: 自建 DDoS 平台 现在有开源的 DDoS 平台源代码,只要有足够机器和带宽资 ...