mysql常用函數(shù)之group_concat()、group by、count()、case when then的使用
場景:
在mysql的關聯(lián)查詢或子查詢中,函數(shù) group_concat(arg) 可以合并多行的某列(或多列)數(shù)據(jù)為一行,默認以逗號分隔。以及分組函數(shù)和統(tǒng)計函數(shù)的組合使用
測試數(shù)據(jù)準備:

一、行轉(zhuǎn)列函數(shù) 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
小結(jié):
1、group_concat() 函數(shù)默認合并后以逗號分隔,也可以自定義分隔符
2、group_concat() 函數(shù)可以多列合并,列和列之間可以自定義分隔符
3、group_concat() 函數(shù)可以使用 distinct 進行去重合并
二、分組 group by、count()、sum() 函數(shù)的組合使用
1、分組和統(tǒng)計
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;輸出:

小結(jié):
1、group by 分組可以配合 count() 統(tǒng)計函數(shù)綜合使用,輸出每組中的數(shù)量
2、group by 分組可以配合 sum() 求和函數(shù)綜合使用,輸出每組中的數(shù)字的和
3、group by 分組可以配合 count()、sum() 一起使用,輸出每組中的數(shù)量以及和
三、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 '創(chuàng)建人Id',
create_date datetime not null default now() comment '創(chuàng)建時間',
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 '是否在職狀態(tài),0-否,1-是',
dept_id bigint null comment '部門Id',
create_by bigint comment '創(chuàng)建人Id',
create_date datetime not null default now() comment '創(chuàng)建時間',
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 '人員資料信息表';
-- 寫入數(shù)據(jù)
INSERT INTO any_case.t_department_info (id, dept_name, dept_director, create_by, create_date, update_by, update_date) VALUES (1, '研發(fā)部', '張三', 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、主從表關聯(lián)查詢統(tǒng)計示例腳本
select tdi.dept_name,
tdi.dept_director
,count(tpi.id) as allPersonNum -- 全部人數(shù)
,count(case when tpi.if_on_job = 1 then tpi.id end) as ifOnJobNum -- 在職全部人數(shù)
,count(case when tpi.if_on_job = 1 and tpi.gender = 'M' then tpi.id end) as ifOnJobMNum -- 在職男性人數(shù)
,count(case when tpi.if_on_job = 1 and tpi.gender = 'F' then tpi.id end) as ifOnJobFNum -- 在職女性人數(shù)
,count(case when tpi.if_on_job = 0 then tpi.id end) as quitNum -- 離職總?cè)藬?shù)
,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 -- 本月離職人數(shù)
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() 中根據(jù)從表中的條件來判斷是否統(tǒng)計入該條數(shù)據(jù),符合條件的話返回給 count() 統(tǒng)計依據(jù)列,不符合條件返回給 count() 統(tǒng)計依據(jù)為 null(默認null不統(tǒng)計)
2、這樣寫的好處比關聯(lián)多個 left join 對象這種方式的查詢效率要快很多,而且還簡潔明了不混亂
到此這篇關于mysql常用函數(shù)之group_concat()、group by、count()、case when then的使用的文章就介紹到這了,更多相關mysql group_concat()、group by、count()、case when then內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Win 8或以上系統(tǒng)下MySQL最新版5.7.17(64bit ZIP綠色版)安裝部署教程
這篇文章主要為大家詳細介紹了Win 8或以上系統(tǒng)下MySQL最新版5.7.17 64bit ZIP綠色版安裝部署教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
Mysql報Table?'mysql.user'?doesn't?exist問題的解
這篇文章主要給大家介紹了關于Mysql報Table?'mysql.user'?doesn't?exist問題的解決方法,初學者可能會遇到這個問題,文中通過圖文將解決方法介紹的非常詳細,需要的朋友可以參考下2022-05-05
php 不能連接數(shù)據(jù)庫 php error Can''t connect to local MySQL server
php 不能連接數(shù)據(jù)庫 php error Can't connect to local MySQL server through socket '/tmp/mysql.sock'2011-05-05
mysql如何分別按年/月/日/周分組統(tǒng)計數(shù)據(jù)詳解
我們在用Mysql抽取數(shù)據(jù)時候,經(jīng)常需要按照天、周、月等不同的粒度對數(shù)據(jù)進行分組統(tǒng)計,下面這篇文章主要給大家介紹了關于mysql如何分別按年/月/日/周分組統(tǒng)計數(shù)據(jù)的相關資料,需要的朋友可以參考下2022-12-12
MySQL5綠色版windows下安裝總結(jié)(推薦)
這篇文章主要介紹了MySQL5綠色版windows下安裝總結(jié),需要的朋友可以參考下2017-03-03
MySQL中having和where的區(qū)別及應用詳解
這篇文章主要給大家詳細介紹了MySQL中having和where的區(qū)別以及他們的使用方法,文中有相關的代碼示例,具有一定的參考價值,需要的朋友可以參考下2023-06-06
IDEA連接MySQL數(shù)據(jù)庫并執(zhí)行SQL語句使用數(shù)據(jù)圖文詳解
使用idea連接本地MySQL數(shù)據(jù)庫,就可以很方便的看到數(shù)據(jù)庫的內(nèi)容,還可以進行基本的增加,刪除,修改操作,下面這篇文章主要給大家介紹了關于IDEA連接MySQL數(shù)據(jù)庫并執(zhí)行SQL語句使用數(shù)據(jù)的相關資料,需要的朋友可以參考下2023-03-03
使用存儲過程實現(xiàn)循環(huán)插入100條記錄
本節(jié)主要介紹了使用存儲過程實現(xiàn)循環(huán)插入100條記錄的具體實現(xiàn),需要的朋友可以參考下2014-07-07

