iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >mysql常用函数之group_concat()、groupby、count()、casewhenthen的使用
  • 132
分享到

mysql常用函数之group_concat()、groupby、count()、casewhenthen的使用

mysqlgroup_concat()groupbycount()casewhenthen 2023-01-04 15:01:28 132人浏览 薄情痞子
摘要

目录场景:一、行转列函数 group_concat(arg)二、分组 group by、count()、sum() 函数的组合使用三、count() 配

场景:

Mysql的关联查询或子查询中,函数 group_concat(arg) 可以合并多行的某列(或多列)数据为一行,默认以逗号分隔。以及分组函数和统计函数的组合使用

测试数据准备:

一、行转列函数 group_concat(arg)

1、单列合并,默认以逗号分隔

select
    group_concat(ttop.user_name) as testStr
from t_table_one_parent ttop;

输出:
张三1,张三2,张三3,张三1,张三2,张三3,张三4

2、单列合并,指定冒号分隔符

select
    group_concat(ttop.user_name separator ';') as testStr
from t_table_one_parent ttop;

输出:
张三1;张三2;张三3;张三1;张三2;张三3;张三4

3、单列合并,并去重

select
    group_concat(distinct ttop.user_name separator ';') as testStr
from t_table_one_parent ttop;

输出:
张三1;张三2;张三3;张三4

4、多列拼接合并

select
    group_concat(distinct ttop.user_name, ttop.company_code separator ';') as testStr
from t_table_one_parent ttop;

输出:
张三1123456;张三21234567;张三312345678;张三4123456789

5、多列拼接合并,列和列之间指定分隔符

select
    group_concat(distinct ttop.user_name, ',', ttop.company_code separator ';') as testStr
from t_table_one_parent ttop;

输出:
张三1,123456;张三2,1234567;张三3,12345678;张三4,123456789

小结:
1、group_concat() 函数默认合并后以逗号分隔,也可以自定义分隔符
2、group_concat() 函数可以多列合并,列和列之间可以自定义分隔符
3、group_concat() 函数可以使用 distinct 进行去重合并

二、分组 group by、count()、sum() 函数的组合使用

1、分组和统计

select
     user_name as userName,
     count(user_name) as ctUserName
from t_table_one_parent ttop group by user_name;

输出:

2、分组和求和

select
     user_name as userName,
     count(user_name) as ctUserName,
     sum(total_account_balance) as sumTab
from t_table_one_parent ttop group by user_name;

输出:

小结:
1、group by 分组可以配合 count() 统计函数综合使用,输出每组中的数量
2、group by 分组可以配合 sum() 求和函数综合使用,输出每组中的数字的和
3、group by 分组可以配合 count()、sum() 一起使用,输出每组中的数量以及和

三、count() 配合 case when then 的使用

脚本备份:

create table if not exists t_department_info
(
    id            bigint      not null primary key auto_increment comment '主键id',
    dept_name     varchar(50) not null comment '部门名称',
    dept_director varchar(20) not null comment '部门主管',
    create_by     bigint comment '创建人Id',
    create_date   datetime    not null default now() comment '创建时间',
    update_by     bigint comment '更新人Id',
    update_date   datetime    not null default now() on update now() comment '更新时间'
) engine = InnoDB
  auto_increment = 1
  default charset = utf8 comment '部门信息表';
 
 
create table if not exists t_person_info
(
    id             bigint      not null primary key auto_increment comment '主键id',
    person_name    varchar(10) not null comment '人员名称',
    id_number      varchar(50) not null comment '省份证号',
    gender         varchar(5)  not null comment '性别,M男、F女',
    induction_date datetime    null comment '入职日期',
    quit_date      datetime    null comment '离职日期',
    if_on_job      tinyint(1)           default 1 comment '是否在职状态,0-否,1-是',
    dept_id        bigint      null comment '部门Id',
    create_by      bigint comment '创建人Id',
    create_date    datetime    not null default now() comment '创建时间',
    update_by      bigint comment '更新人Id',
    update_date    datetime    not null default now() on update now() comment '更新时间'
) engine = InnoDB
  auto_increment = 1
  default charset = utf8 comment '人员资料信息表';
 
 
 
 
 
 
-- 写入数据
INSERT INTO any_case.t_department_info (id, dept_name, dept_director, create_by, create_date, update_by, update_date) VALUES (1, '研发部', '张三', 1, '2022-12-22 16:38:10', null, '2022-12-22 16:38:10');
INSERT INTO any_case.t_department_info (id, dept_name, dept_director, create_by, create_date, update_by, update_date) VALUES (2, '测试部', '张三', 1, '2022-12-22 16:38:10', null, '2022-12-22 16:38:10');
INSERT INTO any_case.t_department_info (id, dept_name, dept_director, create_by, create_date, update_by, update_date) VALUES (3, '运维部', '李四', 1, '2022-12-22 16:38:10', null, '2022-12-22 16:38:10');
 
 
INSERT INTO any_case.t_person_info (id, person_name, id_number, gender, induction_date, quit_date, if_on_job, dept_id, create_by, create_date, update_by, update_date) VALUES (1, '张三', '123456789987654321', 'M', '2022-11-23 00:40:35', null, 1, 1, 1, '2022-12-22 16:40:48', null, '2022-12-22 16:40:48');
INSERT INTO any_case.t_person_info (id, person_name, id_number, gender, induction_date, quit_date, if_on_job, dept_id, create_by, create_date, update_by, update_date) VALUES (2, '李四', '123456789987654321', 'F', '2022-11-23 00:40:35', '2022-12-23 00:54:47', 0, 1, 1, '2022-12-22 16:40:48', null, '2022-12-22 16:54:40');
INSERT INTO any_case.t_person_info (id, person_name, id_number, gender, induction_date, quit_date, if_on_job, dept_id, create_by, create_date, update_by, update_date) VALUES (3, '王五', '123456789987654321', 'M', '2022-11-23 00:40:35', '2022-11-30 00:54:54', 0, 1, 1, '2022-12-22 16:40:48', null, '2022-12-23 02:13:29');
INSERT INTO any_case.t_person_info (id, person_name, id_number, gender, induction_date, quit_date, if_on_job, dept_id, create_by, create_date, update_by, update_date) VALUES (4, '赵六', '123456789987654321', 'F', '2022-11-23 00:40:35', null, 1, 2, 1, '2022-12-22 16:40:48', null, '2022-12-22 16:40:48');
INSERT INTO any_case.t_person_info (id, person_name, id_number, gender, induction_date, quit_date, if_on_job, dept_id, create_by, create_date, update_by, update_date) VALUES (5, '李七', '123456789987654321', 'M', '2022-11-23 00:40:35', null, 1, 2, 1, '2022-12-22 16:40:48', null, '2022-12-22 16:40:48');
INSERT INTO any_case.t_person_info (id, person_name, id_number, gender, induction_date, quit_date, if_on_job, dept_id, create_by, create_date, update_by, update_date) VALUES (6, '郑八', '123456789987654321', 'F', '2022-11-23 00:40:35', null, 1, 1, 1, '2022-12-22 16:41:17', null, '2022-12-22 17:00:22');

1、主从表关联查询统计示例脚本

select tdi.dept_name,
       tdi.dept_director
       ,count(tpi.id) as allPersonNum	-- 全部人数
       ,count(case when tpi.if_on_job = 1 then tpi.id end) as ifOnJobNum -- 在职全部人数
       ,count(case when tpi.if_on_job = 1 and tpi.gender = 'M' then tpi.id end) as ifOnJobMNum -- 在职男性人数
       ,count(case when tpi.if_on_job = 1 and tpi.gender = 'F' then tpi.id end) as ifOnJobFNum -- 在职女性人数
       ,count(case when tpi.if_on_job = 0 then tpi.id end) as quitNum -- 离职总人数
       ,count(case when tpi.if_on_job = 0 and date_fORMat(tpi.quit_date, '%Y-%m') = date_format(now(), '%Y-%m') then tpi.id end) as quitNumThisMonth -- 本月离职人数
from t_department_info tdi
left join t_person_info tpi on tpi.dept_id = tdi.id
#支持主表和从表过滤
where tdi.dept_director like '%张%'
  and (tpi.if_on_job = 0 and date_format(tpi.quit_date, '%Y-%m') = date_format(now(), '%Y-%m')) > 0
  and tpi.person_name like '%李%'
group by tdi.dept_name, tdi.dept_director;

可见主与从表关系为一对多,而查询列中的 count() 中根据从表中的条件来判断是否统计入该条数据,符合条件的话返回给 count() 统计依据列,不符合条件返回给 count() 统计依据为 null(默认null不统计)

2、这样写的好处比关联多个 left join 对象这种方式的查询效率要快很多,而且还简洁明了不混乱

到此这篇关于mysql常用函数之group_concat()、group by、count()、case when then的使用的文章就介绍到这了,更多相关mysql group_concat()、group by、count()、case when then内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

您可能感兴趣的文档:

--结束END--

本文标题: mysql常用函数之group_concat()、groupby、count()、casewhenthen的使用

本文链接: https://www.lsjlt.com/news/176713.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • mysql常用函数之group_concat()、groupby、count()、casewhenthen的使用
    目录场景:一、行转列函数 group_concat(arg)二、分组 group by、count()、sum() 函数的组合使用三、count() 配...
    99+
    2023-01-04
    mysql group_concat() group by count() case when then
  • mysql常用函数之group_concat()、group by、count()、case when then的使用
    目录场景:一、行转列函数 group_concat(arg)二、分组 group by、count()、sum() 函数的组合使用三、count() 配合 case when then&nb...
    99+
    2023-01-04
    mysql group_concat() groupby count() casewhenthen
  • mysql的count()函数怎么使用
    这篇文章主要介绍“mysql的count()函数怎么使用”,在日常操作中,相信很多人在mysql的count()函数怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”my...
    99+
    2024-04-02
  • MySQL Count函数如何使用
    本文小编为大家详细介绍“MySQL Count函数如何使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“MySQL Count函数如何使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。COU...
    99+
    2023-07-04
  • mysql的concat()函数如何用
    这篇文章主要介绍了mysql的concat()函数如何用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇mysql的concat()函数如何用文章都会有所收获...
    99+
    2024-04-02
  • MySQL中Count函数怎么使用
    这篇文章主要介绍了MySQL中Count函数怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇MySQL中Count函数怎么使用文章都会有所收获,下面我们一起来看看吧。 ...
    99+
    2022-12-07
    mysql count
  • 如何在MySQL中使用count聚合函数
    如何在MySQL中使用count聚合函数?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。一、 基本使用count的基本作用是有两个:统计某个列的...
    99+
    2024-04-02
  • pandas中连接函数concat()函数的使用方法
    这篇文章给大家分享的是有关pandas中连接函数concat()函数的使用方法的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。python中pandas库可以对相关的文件或文本数据进行的读取操作,对于想要...
    99+
    2023-06-14
  • PHP中使用count()函数的方法
    这篇文章主要介绍了PHP中使用count()函数的方法,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。count()语法count  ( mixe...
    99+
    2023-06-14
  • SQL窗口函数之聚合窗口函数的使用(count,max,min,sum)
    目录案例分析1.移动平均值2.累计求和(ROW)3.累计求和(RANGE)示例表和脚本关于窗口函数的基础,请看文章SQL窗口函数 许多常见的聚合函数也可以作为窗口函数使用,包括AVG...
    99+
    2024-04-02
  • mysql 中count函数的作用是什么
    mysql 中count函数的作用是什么,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。SELECT COUNT(salary...
    99+
    2024-04-02
  • python DataFrame数据分组统计groupby()函数的使用
    目录groupby()函数1. groupby基本用法1.1 一级分类_分组求和1.2 二级分类_分组求和1.3 对DataFrameGroupBy对象列名索引(对指定列统计计算)2...
    99+
    2024-04-02
  • jmeter学习指南之常用函数的使用
    说明:本文内容是基于jmeter3.0版本来编写,不同版本可能会有个别部分不一致,但是不会差别太大的。JMeter提供了很多函数,如果能够熟练使用,可以为脚本带来很多方便。JMeter函数是一种特殊值,可用于除测试计划外的任何组件。函数调用...
    99+
    2023-06-05
  • python中分组函数groupby和分组运算函数agg的使用
    目录groupby:agg:今天来介绍pandas中一个很有用的函数groupby,其实和hive中的groupby的效果是一样的,区别在于两种语言的写法问题。groupby在Pyt...
    99+
    2024-04-02
  • mybatis-plus使用sum,count,distinct等函数的方法
    mybatis-plus使用sum,count,distinct等函数的方法 通过mybatis-plus实现以下sql查询 SELECT COUNT(DISTINCT user_name)FROM user_infoWHER...
    99+
    2023-08-16
    mybatis java mysql
  • MySQL常见数值函数怎么使用
    本篇内容主要讲解“MySQL常见数值函数怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“MySQL常见数值函数怎么使用”吧!绝对值函数语法格式:ABS(X...
    99+
    2023-03-02
    mysql
  • MySQL的常用函数大全
    一、字符串函数 常用函数: 函数功能CONCAT(s1, s2, …, sn)字符串拼接,将s1, s2, …, sn拼接成一个字符串LOWER(str)将字符串全部转为小写UPPER(str)将字符串全部转为大写LPAD...
    99+
    2023-08-31
    mysql 数据库
  • Python数据分析之NumPy常用函数使用详解
    目录文件读入1、保存或创建新文件 2、读取csv文件的函数loadtxt3、常见的函数4、股票的收益率等5、对数收益与波动率6、日期分析总结本篇我们将以分析历史股价为例,介...
    99+
    2024-04-02
  • MySQL常用函数的用法介绍
    这篇文章主要介绍“MySQL常用函数的用法介绍”,在日常操作中,相信很多人在MySQL常用函数的用法介绍问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”MySQL常用函数的用法...
    99+
    2024-04-02
  • 常用的MySQL函数有哪些
    这篇文章主要介绍了常用的MySQL函数有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。函数 0. 显示当前时间命令:selec...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作