一:group_concat函数详解

1.语法如下:

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

2.基本查询:

select * from aa;  

结果:

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

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

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)

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

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)

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

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)

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

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函数与find_in_set()

1.直接看例子明了,首先看表的内容

2.选出name='yhb'的记录

 select name,group_concat(id) id from test where name='yhb';

结果为:

3.使用find_in_set筛选出name='yhb'的记录,当然这只是为了演示函数有什么效果

 select a.* from test a
join ( select name,group_concat(id) id from test where name='yhb') b on find_in_set(a.id,b.id)
order by a.id;

结果为:

4.使用!find_in_set筛选出name!='yhb'的记录

select a.* from test a
join ( select name,group_concat(id) id from test where name='yhb') b on !find_in_set(a.id,b.id)
order by a.id;

结果为:

程序猿必读

group_concat函数与find_in_set()函数相结合的更多相关文章

  1. 【mysql函数】FIND_IN_SET函数用法

    当你的数据存储为一下格式时,想查出带有某个id的所有数据时,FIND_IN_SET这个函数可以帮到你. ',C_BranchId)

  2. mysql中find_in_set()函数的使用

    首先举个例子来说: 有个文章表里面有个type字段,它存储的是文章类型,有 1头条.2推荐.3热点.4图文等等 .现在有篇文章他既是头条,又是热点,还是图文,type中以 1,3,4 的格式存储.那我 ...

  3. MySQL 的 find_in_set 函数使用方法

    举个例子来说: 有个文章表里面有个type字段,它存储的是文章类型,有 1头条.2推荐.3热点.4图文...1,12,13 等等 . 现在有篇文章他既是 头条,又是热点,还是图文, type中以 1, ...

  4. Mysql中使用find_in_set函数查找字符串

    mysql有个表的字段的存储是以逗号分隔的,如domain字段login.s01.yy.com,s01.yy.com,s02.yy.com.现在要查找s01.yy.com这个.我们用like查找好像不 ...

  5. mysql 的 find_in_set函数使用方法

    举个例子来说: 有个文章表里面有个type字段,他存储的是文章类型,有 1头条,2推荐,3热点,4图文 .....11,12,13等等 现在有篇文章他既是 头条,又是热点,还是图文, type中以 1 ...

  6. (转)mysql 的 find_in_set函数使用方法

    举个例子来说: 有个文章表里面有个type字段,他存储的是文章类型,有 1头条,2推荐,3热点,4图文 .....11,12,13等等 现在有篇文章他既是 头条,又是热点,还是图文, type中以 1 ...

  7. MySQL的FIND_IN_SET()函数

    今天在做项目时,看到了一个从没见过的MySQL函数——FIND_IN_SET(),顿时就产生了浓郁的兴趣,然后就搜了搜,翻了翻. 语法:FIND_IN_SET(str,strlist) 定义: 1. ...

  8. Yii笔记:打印sql、Form表单、时间插件、Mysql的 FIND_IN_SET函数使用、是否是post/ajax请求

    语句部分: yii1版本打印最后一条执行的SQL: $this->getDbConnection()->createCommand()->select()->from()-&g ...

  9. mysql find_in_set()函数的使用

    mysql 中 find_in_set() 函数语法: FIND_IN_SET(str,strList) str 要查询的字符串 strList 字段名,参数以“,”分隔,如(1,2,6,8) 查询字 ...

随机推荐

  1. 二叉树hdu1710

    学习二叉树,看了两天也不明白,唉!acm之路让我体验到要付出巨大的努力,废话不多说,看我网上找到的代码: 此题题意很明确,给你先序遍历,中序遍历,求后序遍历.但代码就让我找不到东西了. http:// ...

  2. 6.mybatis异常:SQL Mapper Configuration,Error parsing Mapper XML,Could not resolve type alias

    在xxxMapper中 <select id="getClazz" parameterType="int" resultType="getCla ...

  3. Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2)C. Road to Cinema 二分

    C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. hdu 5693 朋友 博弈

    朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Descr ...

  5. Windows Internals学习笔记(五)Synchronization

    参考资料: 1. <Windows Internals> 2. 自旋锁spinlock剖析与改进 3. Lock指令前缀 4. Lock指令前缀(二) 5. Kernel Dispatch ...

  6. maven2 配置M2_REPO

    从eclipse中增加了maven2的插件之后,maven默认的本地库的路径是${user}/.m2/repository/下,一般windows用户的操作系统都安装在C盘,所以这个目录 下的jar包 ...

  7. HDU-4507 吉哥系列故事——恨7不成妻 数位DP

    题意:给定区间[L, R]求区间内与7无关数的平方和.一个数当满足三个规则之一则认为与7有关:1.整数中某一位是7:2.整数的每一位加起来的和是7的整数倍:3.这个整数是7的整数倍: 分析:初看起来确 ...

  8. Redis基础知识之————php-Redis 常用命令专题

    Keys del,delete - 删除键 dump - 返回存储在指定键值的序列化版本. exists - 确定键是否存在 expire,setTimeout,pexpire - 设置键的生存时间( ...

  9. mysql概要(十五)存储过程

    1.定义: 2.查看所有存储过程: show procedure status; 3.创建存储过程: create procedure 存储过程名字(参数) begin s1l语句; end$     ...

  10. 关于json 的那些知识点

    深入理解JSON对象 前面的话 json(javascript object notation)全称是javascript对象表示法,它是一种数据交换的文本格式,而不是一种编程语言,用于读取结构化数据 ...