来自: http://hchmsguo.iteye.com/blog/555543

MySQL中group_concat函数

完整的语法如下:

group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])

基本查询

  1. select * from aa;

+------+------+
| id| name |
+------+------+
|1 | 10|
|1 | 20|
|1 | 20|
|2 | 20|
|3 | 200 |
|3 | 500 |
+------+------+
6 rows in set (0.00 sec)

以id分组,把name字段的值打印在一行,逗号分隔(默认)

  1. select id,group_concat(name) from aa group by id;

+------+--------------------+
| id| group_concat(name) |
+------+--------------------+
|1 | 10,20,20|
|2 | 20 |
|3 | 200,500|
+------+--------------------+
3 rows in set (0.00 sec)

以id分组,把name字段的值打印在一行,分号分隔

  1. select id,group_concat(name separator ';') from aa group by id;

+------+----------------------------------+
| id| group_concat(name separator ';') |
+------+----------------------------------+
|1 | 10;20;20 |
|2 | 20|
|3 | 200;500 |
+------+----------------------------------+
3 rows in set (0.00 sec)

以id分组,把去冗余的name字段的值打印在一行,

逗号分隔

  1. select id,group_concat(distinct name) from aa group by id;

+------+-----------------------------+
| id| group_concat(distinct name) |
+------+-----------------------------+
|1 | 10,20|
|2 | 20 |
|3 | 200,500 |
+------+-----------------------------+
3 rows in set (0.00 sec)

以id分组,把name字段的值打印在一行,逗号分隔,以name排倒序

  1. select id,group_concat(name order by name desc) from aa group by id;

+------+---------------------------------------+
| id| group_concat(name order by name desc) |
+------+---------------------------------------+
|1 | 20,20,10 |
|2 | 20|
|3 | 500,200|
+------+---------------------------------------+
3 rows in set (0.00 sec)

group_concat函数详解的更多相关文章

  1. 【转】group_concat函数详解

    转自: http://hchmsguo.iteye.com/blog/555543 问了好多人,都不知道group_concat这个函数. 这个函数好啊,能将相同的行组合起来,省老事了. MySQL中 ...

  2. 转 group_concat函数详解

    MySQL中group_concat函数 完整的语法如下: group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔 ...

  3. mysql之group_concat函数详解

    函数语法: group_concat( [DISTINCT]  要连接的字段   [Order BY 排序字段 ASC/DESC]   [Separator '分隔符'] ) 下面举例说明: sele ...

  4. Mysql中关于 group_concat函数详解

    group_concat()主要功能:能将相同的行组合起来 完整的语法如下: group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Sepa ...

  5. mysql group_concat函数详解

    group_concat( [DISTINCT]  要连接的字段   [Order BY 排序字段 ASC/DESC]   [Separator '分隔符'] ) 1. --以id分组,把price字 ...

  6. malloc 与 free函数详解<转载>

    malloc和free函数详解   本文介绍malloc和free函数的内容. 在C中,对内存的管理是相当重要.下面开始介绍这两个函数: 一.malloc()和free()的基本概念以及基本用法: 1 ...

  7. NSSearchPathForDirectoriesInDomains函数详解

    NSSearchPathForDirectoriesInDomains函数详解     #import "NSString+FilePath.h" @implementation ...

  8. JavaScript正则表达式详解(二)JavaScript中正则表达式函数详解

    二.JavaScript中正则表达式函数详解(exec, test, match, replace, search, split) 1.使用正则表达式的方法去匹配查找字符串 1.1. exec方法详解 ...

  9. Linux C popen()函数详解

    表头文件 #include<stdio.h> 定义函数 FILE * popen( const char * command,const char * type); 函数说明 popen( ...

随机推荐

  1. C++使用protobuf传输中间包含\0的字节数组

    The C++ implementation of protocol buffers returns the byte and string types as std::string. This st ...

  2. 刷CM7固件 乐padA1-07专用固件

    --------------------------------------------------------------------------------               前几天在版 ...

  3. SQL Lazy Spool Eager Spool

    https://www.simple-talk.com/sql/learn-sql-server/showplan-operator-of-the-week-lazy-spool/ The Lazy ...

  4. windows 下 node.js 和 express 的安装

    下载 node 下载和安装 下载地址 https://nodejs.org/en/ 下载文件 node-v4.5.0-x64.msi nodejs 安装 express -g 代表全局安装 npm i ...

  5. MonoDev 冷门而好用的功能

    [MonoDev 冷门而好用的功能] 1.Ctrl + B.跳转到对应的{}.(). 2.View -> Editor Layout -> 2 Columns.双屏工作. 3.Editr ...

  6. 初学c# -- 学习笔记(七) RichTextBox支持GIF

    园子里许明吉博客写的一篇,刚好用到这个,写的非常好.转过来了 不过在应用中也有一些问题,win10下不能中文输入,凑合着进行了修改, 下面是原来的代码: private void button2_Cl ...

  7. I/O流——字符流

    字符流 字节流提供处理任何类型输入/输出操作的足够功能,但不能直接操作Unicode字符,因而需要字符流. 字符流层次结构的顶层是Reader和Writer抽象类. 实际上,字符流的底层就是字节流. ...

  8. ajax将json写到table中去

    查询条件: <table style="width: 100%;border-collapse: collapse;" > <tr> <th styl ...

  9. leveldb 学习笔记之VarInt

    在leveldb在查找比较时的key里面保存key长度用的是VarInt,何为VarInt呢,就是变长的整数,每7bit代表一个数,第8bit代表是否还有下一个字节, 1. 比如小于128(一个字节以 ...

  10. Validate US Telephone Numbers

    function telephoneCheck(str) { // Good luck! //return true; var phone = /^1? ?(\d{3}|\(\d{3}\))[ -]? ...